<!--  参数配置-->
<template>
    <el-table :data="existingTasklist" style="width: 100%">
        <el-table-column prop="hyperParameter" label="超参数" width="150">
        </el-table-column>
        <el-table-column prop="numericalValue" label="数值" >
         <template slot-scope="scope">
          <component
          v-if="configData"
          :ref="scope.row.numericalValue"
          :is="scope.row.componentOptions.component"
          style="width: 100%"
          v-bind="scope.row.componentOptions.property"
          :value="configData[scope.row.numericalValue]||scope.row.defaultValue"
          :size="defaultFormItemSize"
          @input=" e => {changeValue(e, scope.row.numericalValue);}"></component>
        </template>
        </el-table-column>
        <el-table-column prop="explain" label="说明">
        </el-table-column>
   </el-table>
</template>

<script>
export default {
  data () {
    return {

      configData: undefined,
      parameterConfiguration: [
        {
          hyperParameter: '量化等级',
          defaultValue: 'none',
          numericalValue: 'quantizationLevel',
          explain: '启用 4/8 比特模型量化（QLoRA）。',
          componentOptions: {
            component: 'el-cascader',
            property:
             {
               options: [{
                 value: 'none',
                 label: 'none'
               }, {
                 value: '8',
                 label: '8'
               }, {
                 value: '4',
                 label: '4'
               }],
               props: { emitPath: false }
             }
          }
        },
        {
          hyperParameter: '提示模板',
          defaultValue: 'chatglm3',
          numericalValue: 'promptTemplate',
          explain: '构建提示词时使用的模板',
          componentOptions: {
            component: 'el-cascader',
            property:
             {
               options: [
                 {
                   value: 'alpaca',
                   label: 'alpaca'
                 }, {
                   value: 'aquila',
                   label: 'aquila'
                 }, {
                   value: 'baichuan',
                   label: 'baichuan'
                 }, {
                   value: 'baichuan2',
                   label: 'baichuan2'
                 }, {
                   value: 'belle',
                   label: 'belle'
                 }, {
                   value: 'bluelm',
                   label: 'bluelm'
                 }, {
                   value: 'chatglm2',
                   label: 'chatglm2'
                 }, {
                   value: 'chatglm3',
                   label: 'chatglm3'
                 }, {
                   value: 'chatglm3_raw',
                   label: 'chatglm3_raw'
                 }, {
                   value: 'deepseek',
                   label: 'deepseek'
                 }, {
                   value: 'default',
                   label: 'default'
                 }, {
                   value: 'falcon',
                   label: 'falcon'
                 }, {
                   value: 'intern',
                   label: 'intern'
                 }, {
                   value: 'llama2',
                   label: 'llama2'
                 }, {
                   value: 'llama2_zh',
                   label: 'llama2_zh'
                 }, {
                   value: 'mistral',
                   label: 'mistral'
                 }, {
                   value: 'openchat',
                   label: 'openchat'
                 }, {
                   value: 'qwen',
                   label: 'qwen'
                 }, {
                   value: 'starchat',
                   label: 'starchat'
                 }, {
                   value: 'vanilla',
                   label: 'vanilla'
                 }, {
                   value: 'vicuna',
                   label: 'vicuna'
                 }, {
                   value: 'xverse',
                   label: 'xverse'
                 }, {
                   value: 'yayi',
                   label: 'yayi'
                 }, {
                   value: 'zephyr',
                   label: 'zephyr'
                 }, {
                   value: 'ziya',
                   label: 'ziya'
                 }],
               props: { emitPath: false }
             }
          }
        },
        {
          hyperParameter: '截断长度',
          defaultValue: 1024,
          numericalValue: 'truncationLength',
          explain: '输入序列分词后的最大长度。',
          componentOptions: {
            component: 'el-input-number',
            property:
             {
               min: 4,
               max: 8192
               
             }
          }
        },
        {
          hyperParameter: '学习率',
          defaultValue: 5e-5,
          numericalValue: 'learningRate',
          explain: '学习率（LearningRate）是在梯度下降的过程中更新权重时的超参数，过高会导致模型难以收敛，过低则会导致模型收敛速度过慢，平台已给出默认推荐值，可根据经验调整。',
          componentOptions: {
            component: 'el-input-number',
            property:
             {
               max: 0.0002,
               min: 2e-7
             }
          }
        },
        {
          hyperParameter: '迭代轮次',
          numericalValue: 'iterationRound',
          defaultValue: 3,
          explain: '迭代轮次（Epoch），控制训练过程中的迭代轮数。',
          componentOptions: {
            component: 'el-input-number',
            property:
             {
               max: 500,
               min: 0
             }
          }
        },
        {
          hyperParameter: '最大样本数',
          defaultValue: 100000,
          numericalValue: 'maximumSampleSize',
          explain: '每个数据集最多使用的样本数',
          componentOptions: {
            component: 'el-input-number',
            property:
             {
               max: 100000,
               min: 0
             }
          }
        },
        {
          hyperParameter: '计算类型',
          numericalValue: 'CalculationType',
          defaultValue: 'fp16',
          explain: '是否启用 FP16 或 BF16 混合精度训练。',
          componentOptions: {
            component: 'el-cascader',
            property:
             {
               options: [{
                 value: 'fp16',
                 label: 'fp16'
               }, {
                 value: 'bf16',
                 label: 'bf16'
               }],
               props: { emitPath: false }
             }
          }
        },
        {
          hyperParameter: '批处理大小',
          numericalValue: 'batchSize',
          defaultValue: 4,
          explain: '批处理大小（BatchSize）表示在每次训练迭代中使用的样本数。较大的批处理大小可以加速训练，但可能会导致内存问题。',
          componentOptions: {
            component: 'el-input-number',
            property:
             {
               max: 512,
               min: 1
             }
          }
        },
  
        {
          hyperParameter: '梯度累积',
          defaultValue: 4,
          numericalValue: 'gradientAccumulation',
          explain: '梯度累积的步数。',
          componentOptions: {
            component: 'el-input-number',
            property:
             {
               max: 512,
               min: 1
             }
          }
        },
        {
          hyperParameter: '学习率调节器',
          defaultValue: 'cosine',
          numericalValue: 'LearningRateRegulator',
          explain: '采用的学习率调节器名称。',
          componentOptions: {
            component: 'el-cascader',
            property:
             {
               options: [{
                 value: 'linear',
                 label: 'linear'
               }, {
                 value: 'cosine',
                 label: 'cosine'
               }, {
                 value: 'cosine_with_restarts',
                 label: 'cosine_with_restarts'
               }, {
                 value: 'polynomial',
                 label: 'polynomial'
               }, {
                 value: 'constant',
                 label: 'constant'
               }, {
                 value: 'constant_with_warmup',
                 label: 'constant_with_warmup'
               }, {
                 value: 'inverse_sqrt',
                 label: 'inverse_sqrt'
               }, {
                 value: 'reduce_lr_on_plateau',
                 label: 'reduce_lr_on_plateau'
               }],
               props: { emitPath: false }
             }
          }
        },
  
        {
          hyperParameter: '最大梯度范数',
          defaultValue: 1,
          numericalValue: 'maximumGradientNorm',
          explain: '用于梯度裁剪的范数。',
          componentOptions: {
            component: 'el-input-number',
            property:
             {
               max: 100000,
               min: 0
             }
          }
        },
  
        {
          hyperParameter: '验证集比例',
          defaultValue: 0,
          numericalValue: 'verificationRatio',
          explain: '验证集占全部样本的百分比。',
          componentOptions: {
            component: 'el-input-number',
            property:
             {
               max: 1,
               min: 0
             }
          }
        },
  
        {
          hyperParameter: '日志间隔',
          defaultValue: 5,
          numericalValue: 'LogInterval',
          explain: '每两次日志输出间的更新步数。',
          componentOptions: {
            component: 'el-input-number',
            property:
             {
               max: 1000,
               min: 5
             }
          }
        },
  
        {
          hyperParameter: '保存间隔',
          defaultValue: 100,
          numericalValue: 'saveInterval',
          explain: '每两次断点保存间的更新步数。',
          componentOptions: {
            component: 'el-input-number',
            property:
             {
               max: 5000,
               min: 10
             }
          }
        },
        {
          hyperParameter: '预热步数',
          defaultValue: 0,
          numericalValue: 'warmUpSteps',
          explain: '学习率预热采用的步数。',
          componentOptions: {
            component: 'el-input-number',
            property:
             {
               max: 5000,
               min: 0
             }
          }
        },
        {
          hyperParameter: 'NEFTune 噪声参数',
          defaultValue: 0,
          numericalValue: 'nEFTuneNoiseParameter',
          explain: '嵌入向量所添加的噪声大小。',
          componentOptions: {
            component: 'el-input-number',
            property:
             {
               max: 10,
               min: 0
             }
          }
        },
        {
          hyperParameter: 'LoRA 秩',
          defaultValue: 8,
          numericalValue: 'loRaRank',
          explain: 'LoRA 矩阵的秩。',
          componentOptions: {
            component: 'el-input-number',
            property:
             {
               max: 1024,
               min: 0
             }
          }
        },
        {
          hyperParameter: 'LoRA 随机丢弃',
          defaultValue: 0.1,
          numericalValue: 'loRaRandomDiscard',
          explain: 'LoRA 权重随机丢弃的概率。',
          componentOptions: {
            component: 'el-input-number',
            property:
             {
               max: 1,
               min: 0
             }
          }
        },
        {
          hyperParameter: 'DPO beta 参数',
          defaultValue: 0.1,
          numericalValue: 'dpoBetaData',
          explain: 'DPO 损失函数中 beta 超参数大小。',
          componentOptions: {
            component: 'el-input-number',
            property:
             {
               max: 1,
               min: 0
             }
          }
        }
      ]
    };
  },
  props: ['existingTask', 'value'],
  components: {},

  computed: {
    existingTasklist () {
      return this.parameterConfiguration
    }
    
  },

  mounted () {
    this.$nextTick(() => {
      this.init();
    })
  },

  methods: {
    getProperty () {

    },
    init () {
      let initConfig = {}
      this.parameterConfiguration.forEach((item) => {
        initConfig[item.numericalValue] = item.defaultValue
      })
      if (this.isJSON(this.value)) {
        this.configData = JSON.parse(this.value) || initConfig
      } else {
        this.configData = initConfig
      }
      this.$emit('input', JSON.stringify(this.configData));
    },
    isJSON (str) {
      try {
        JSON.parse(str);
      } catch (e) {
        // 转换出错，抛出异常
        return false;
      }
      return true;
    },
    changeValue (value, numericalValue) {
      this.configData[numericalValue] = value
      this.$emit('input', JSON.stringify(this.configData));
    }
  }
}

</script>
<style >
div /deep/ .el-cascader-menu__wrap{
  height: auto;
}
</style>
{"quantizationLevel":null,"promptTemplate":null,"truncationLength":1024,"learningRate":0.00005,"iterationRound":3,"maximumSampleSize":100000,"CalculationType":null,"batchSize":4,"gradientAccumulation":4,"LearningRateRegulator":null,"maximumGradientNorm":1,"verificationRatio":0,"LogInterval":5,"saveInterval":100,"warmUpSteps":0,"nEFTuneNoiseParameter":0,"loRaRank":8,"loRaRandomDiscard":0.1,"dpoBetaData":0.1}
