class_docstr: 用来识别 topk 模型并保存，也可以仅当一个保存 Saver 使用。保存路径为::
  
      - folder/
          - YYYY-mm-dd-HH_MM_SS_fffff/  # 自动根据当前脚本的启动时间创建的
              - {save_object}-epoch_{epoch_idx}-batch_{global_batch_idx}-{topk_monitor}_{monitor_value}/  # 满足topk条件存储文件名
  
  :param topk: 保存表现最好的 ``topk`` 个模型，-1 为保存所有模型；0 为都不保存；大于 0 的数为保存 ``topk`` 个；
  :param monitor: 监控的 metric 值：
  
      * 为 ``None``
       将尝试使用 :class:`~fastNLP.core.controllers.Trainer` 中设置 `monitor` 值（如果有设置）。
      * 为 ``str``
       尝试直接使用该名称从 ``evaluation`` 结果中寻找，如果在 ``evaluation`` 结果中没有找到完全一致的名称，将
       使用 最长公共字符串算法 从 ``evaluation`` 结果中找到最匹配的那个作为 ``monitor`` 。
      * 为 :class:`Callable`
       接受参数为 ``evaluation`` 的结果(字典类型)，返回一个 ``float`` 值作为 ``monitor`` 的结果，如果当前结果中没有相关
       的 ``monitor`` 值请返回 ``None`` 。
  :param larger_better: 该 monitor 是否越大越好。
  :param folder: 保存在哪个文件夹下，默认为当前 folder 下。
  :param save_object: 可选 ``['trainer', 'model']`` ，表示在保存时的保存对象为 ``trainer+model`` 还是 只是 ``model`` 。如果
      保存 ``trainer`` 对象的话，将会保存 :class:`~fastNLP.core.controllers.Trainer` 的相关状态，可以通过 :meth:`Trainer.load_checkpoint` 加载该断
      点继续训练。如果保存的是 ``Model`` 对象，则可以通过 :meth:`Trainer.load_model` 加载该模型权重。
  :param only_state_dict: 保存时是否仅保存权重，在 ``model_save_fn`` 不为 None 时无意义。
  :param model_save_fn: 个性化的保存函数，当触发保存操作时，就调用这个函数，这个函数应当接受一个文件夹作为参数，不返回任何东西。
      如果传入了 ``model_save_fn`` 函数，fastNLP 将不再进行模型相关的保存。在多卡场景下，我们只在 rank 0 上会运行该函数。
  :param save_evaluate_results: 是否保存 evaluate 的结果。如果为 True ，在保存 topk 模型的 folder 中还将额外保存一个
      ``fastnlp_evaluate_results.json`` 文件，记录当前的 metric results 。仅在设置了 ``topk`` 的场景下有用，默认为 True 。
  :param kwargs: 更多需要传递给 :meth:`Trainer.save_checkpoint` 或者 :meth:`Trainer.save_model` 接口的参数。
class_docstr_tok: ["\u7528\u6765\u8bc6\u522b","topk","\u6a21\u578b\u5e76\u4fdd\u5b58\uff0c\u4e5f\u53ef\u4ee5\u4ec5\u5f53\u4e00\u4e2a\u4fdd\u5b58","Saver","\u4f7f\u7528\u3002\u4fdd\u5b58\u8def\u5f84\u4e3a",":",":","-","folder\/","-","YYYY-mm-dd-HH_MM_SS_fffff\/","#","\u81ea\u52a8\u6839\u636e\u5f53\u524d\u811a\u672c\u7684\u542f\u52a8\u65f6\u95f4\u521b\u5efa\u7684","-","{","save_object","}","-epoch_","{","epoch_idx","}","-batch_","{","global_batch_idx","}","-","{","topk_monitor","}","_","{","monitor_value","}","\/","#","\u6ee1\u8db3topk\u6761\u4ef6\u5b58\u50a8\u6587\u4ef6\u540d",":","param","topk",":","\u4fdd\u5b58\u8868\u73b0\u6700\u597d\u7684","``","topk","``","\u4e2a\u6a21\u578b\uff0c-1","\u4e3a\u4fdd\u5b58\u6240\u6709\u6a21\u578b\uff1b0","\u4e3a\u90fd\u4e0d\u4fdd\u5b58\uff1b\u5927\u4e8e","0","\u7684\u6570\u4e3a\u4fdd\u5b58","``","topk","``","\u4e2a\uff1b",":","param","monitor",":","\u76d1\u63a7\u7684","metric","\u503c\uff1a","*","\u4e3a","``","None","``","\u5c06\u5c1d\u8bd5\u4f7f\u7528",":","class",":","`","~fastNLP.core.controllers.Trainer","`","\u4e2d\u8bbe\u7f6e","`","monitor","`","\u503c\uff08\u5982\u679c\u6709\u8bbe\u7f6e\uff09\u3002","*","\u4e3a","``","str","``","\u5c1d\u8bd5\u76f4\u63a5\u4f7f\u7528\u8be5\u540d\u79f0\u4ece","``","evaluation","``","\u7ed3\u679c\u4e2d\u5bfb\u627e\uff0c\u5982\u679c\u5728","``","evaluation","``","\u7ed3\u679c\u4e2d\u6ca1\u6709\u627e\u5230\u5b8c\u5168\u4e00\u81f4\u7684\u540d\u79f0\uff0c\u5c06","\u4f7f\u7528","\u6700\u957f\u516c\u5171\u5b57\u7b26\u4e32\u7b97\u6cd5","\u4ece","``","evaluation","``","\u7ed3\u679c\u4e2d\u627e\u5230\u6700\u5339\u914d\u7684\u90a3\u4e2a\u4f5c\u4e3a","``","monitor","``","\u3002","*","\u4e3a",":","class",":","`","Callable","`","\u63a5\u53d7\u53c2\u6570\u4e3a","``","evaluation","``","\u7684\u7ed3\u679c","(","\u5b57\u5178\u7c7b\u578b",")","\uff0c\u8fd4\u56de\u4e00\u4e2a","``","float","``","\u503c\u4f5c\u4e3a","``","monitor","``","\u7684\u7ed3\u679c\uff0c\u5982\u679c\u5f53\u524d\u7ed3\u679c\u4e2d\u6ca1\u6709\u76f8\u5173","\u7684","``","monitor","``","\u503c\u8bf7\u8fd4\u56de","``","None","``","\u3002",":","param","larger_better",":","\u8be5","monitor","\u662f\u5426\u8d8a\u5927\u8d8a\u597d\u3002",":","param","folder",":","\u4fdd\u5b58\u5728\u54ea\u4e2a\u6587\u4ef6\u5939\u4e0b\uff0c\u9ed8\u8ba4\u4e3a\u5f53\u524d","folder","\u4e0b\u3002",":","param","save_object",":","\u53ef\u9009","``","[","'trainer","'",",","'model","'","]","``","\uff0c\u8868\u793a\u5728\u4fdd\u5b58\u65f6\u7684\u4fdd\u5b58\u5bf9\u8c61\u4e3a","``","trainer+model","``","\u8fd8\u662f","\u53ea\u662f","``","model","``","\u3002\u5982\u679c","\u4fdd\u5b58","``","trainer","``","\u5bf9\u8c61\u7684\u8bdd\uff0c\u5c06\u4f1a\u4fdd\u5b58",":","class",":","`","~fastNLP.core.controllers.Trainer","`","\u7684\u76f8\u5173\u72b6\u6001\uff0c\u53ef\u4ee5\u901a\u8fc7",":","meth",":","`","Trainer.load_checkpoint","`","\u52a0\u8f7d\u8be5\u65ad","\u70b9\u7ee7\u7eed\u8bad\u7ec3\u3002\u5982\u679c\u4fdd\u5b58\u7684\u662f","``","Model","``","\u5bf9\u8c61\uff0c\u5219\u53ef\u4ee5\u901a\u8fc7",":","meth",":","`","Trainer.load_model","`","\u52a0\u8f7d\u8be5\u6a21\u578b\u6743\u91cd\u3002",":","param","only_state_dict",":","\u4fdd\u5b58\u65f6\u662f\u5426\u4ec5\u4fdd\u5b58\u6743\u91cd\uff0c\u5728","``","model_save_fn","``","\u4e0d\u4e3a","None","\u65f6\u65e0\u610f\u4e49\u3002",":","param","model_save_fn",":","\u4e2a\u6027\u5316\u7684\u4fdd\u5b58\u51fd\u6570\uff0c\u5f53\u89e6\u53d1\u4fdd\u5b58\u64cd\u4f5c\u65f6\uff0c\u5c31\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\uff0c\u8fd9\u4e2a\u51fd\u6570\u5e94\u5f53\u63a5\u53d7\u4e00\u4e2a\u6587\u4ef6\u5939\u4f5c\u4e3a\u53c2\u6570\uff0c\u4e0d\u8fd4\u56de\u4efb\u4f55\u4e1c\u897f\u3002","\u5982\u679c\u4f20\u5165\u4e86","``","model_save_fn","``","\u51fd\u6570\uff0cfastNLP","\u5c06\u4e0d\u518d\u8fdb\u884c\u6a21\u578b\u76f8\u5173\u7684\u4fdd\u5b58\u3002\u5728\u591a\u5361\u573a\u666f\u4e0b\uff0c\u6211\u4eec\u53ea\u5728","rank","0","\u4e0a\u4f1a\u8fd0\u884c\u8be5\u51fd\u6570\u3002",":","param","save_evaluate_results",":","\u662f\u5426\u4fdd\u5b58","evaluate","\u7684\u7ed3\u679c\u3002\u5982\u679c\u4e3a","True","\uff0c\u5728\u4fdd\u5b58","topk","\u6a21\u578b\u7684","folder","\u4e2d\u8fd8\u5c06\u989d\u5916\u4fdd\u5b58\u4e00\u4e2a","``","fastnlp_evaluate_results.json","``","\u6587\u4ef6\uff0c\u8bb0\u5f55\u5f53\u524d\u7684","metric","results","\u3002\u4ec5\u5728\u8bbe\u7f6e\u4e86","``","topk","``","\u7684\u573a\u666f\u4e0b\u6709\u7528\uff0c\u9ed8\u8ba4\u4e3a","True","\u3002",":","param","kwargs",":","\u66f4\u591a\u9700\u8981\u4f20\u9012\u7ed9",":","meth",":","`","Trainer.save_checkpoint","`","\u6216\u8005",":","meth",":","`","Trainer.save_model","`","\u63a5\u53e3\u7684\u53c2\u6570\u3002"]
code: def __str__(self):
      return f"topk-{self.topk_queue}#save_object-{self.save_object}"
  
imports: import json
  import os
  from copy import deepcopy
  from pathlib import Path
  from typing import Optional, Dict, Tuple, Callable, Union
  from ...envs.distributed import rank_zero_rm
  from fastNLP.core.log import logger
  from fastNLP.envs import FASTNLP_LAUNCH_TIME
  from fastNLP.envs import rank_zero_call
  from fastNLP.envs.env import FASTNLP_EVALUATE_RESULT_FILENAME
  from .has_monitor_callback import ResultsMonitor
label_desc: Use image node_id 5 for calling the TopkSaver obj's underlying member method code with example usage: obj.__str__() and returns: str, self, str, self