Spaces:
Sleeping
Sleeping
Download data/problems/amp-dynamic-loss-scaler-step.json from dongyanggg/codestar-backend: direct link, hf CLI and curl.
- Browser
- Download file 11 kB
-
https://huggingface.co/spaces/dongyanggg/codestar-backend/resolve/main/data/problems/amp-dynamic-loss-scaler-step.json
- Command line
-
hf download hf://spaces/dongyanggg/codestar-backend/data/problems/amp-dynamic-loss-scaler-step.json
-
curl -L -o amp-dynamic-loss-scaler-step.json https://huggingface.co/spaces/dongyanggg/codestar-backend/resolve/main/data/problems/amp-dynamic-loss-scaler-step.json
11 kB
| { | |
| "slug": "amp-dynamic-loss-scaler-step", | |
| "title": "AMP 混合精度动态损失缩放器 (Dynamic Loss Scaler) 状态机", | |
| "difficulty": "Easy", | |
| "difficulty_breakdown": { | |
| "concept": 2, | |
| "implementation": 2, | |
| "math_derivation": 2, | |
| "boundary_complexity": 2 | |
| }, | |
| "category": "AIInfra", | |
| "tags": [ | |
| "AMP", | |
| "MixedPrecision", | |
| "Training", | |
| "TorchLeet" | |
| ], | |
| "description_md": "### 题目描述\n实现 `torch.cuda.amp.GradScaler` 的动态缩放系数状态更新:若本步梯度出现 `Inf/NaN`(`found_inf=True`),则将 `scale` 乘以 `backoff_factor` 并把连续正常步数计数器 `growth_tracker` 归零;若 `found_inf=False`,则将 `growth_tracker` 加 1,若此时 `growth_tracker == growth_interval`,则将 `scale` 乘以 `growth_factor` 并把 `growth_tracker` 归零。返回 `(new_scale, new_tracker)`。\n\n### 函数签名\n```python\ndef dynamic_loss_scaler_update(\n scale: float,\n growth_tracker: int,\n found_inf: bool,\n growth_factor: float = 2.0,\n backoff_factor: float = 0.5,\n growth_interval: int = 2000,\n) -> Tuple[float, int]:\n```\n\n### 示例\n```python\n# 输入\nscale = 65536.0\ngrowth_tracker = 1500\nfound_inf = True\ngrowth_factor = 2.0\nbackoff_factor = 0.5\ngrowth_interval = 2000\n\n# 输出 (tuple)\n(32768.0, 0)\n```\n\n### 参数规格与约束\n- **`scale`**:`float`\n- **`growth_tracker`**:`int`\n- **`found_inf`**:`bool`\n- **`growth_factor`**:`float`(默认 `2.0`)\n- **`backoff_factor`**:`float`(默认 `0.5`)\n- **`growth_interval`**:`int`(默认 `2000`)\n- **返回值**:`Tuple[float, int]`,输出规格 `Tuple` (`float` (示例值: `32768.0`), `int` (示例值: `0`))\n- **精度容差**:`atol=0.0001, rtol=0.0001`\n> 题目来源:改编自 [TorchLeet](https://github.com/Exorust/TorchLeet)(MIT License),已改写为函数式评测接口与中文题面。", | |
| "entry_point": "dynamic_loss_scaler_update", | |
| "starter_code": "import torch\nimport torch.nn as nn\nimport torch.nn.functional as F\nimport math\nfrom typing import List, Tuple, Dict, Optional\n\ndef dynamic_loss_scaler_update(\n scale: float,\n growth_tracker: int,\n found_inf: bool,\n growth_factor: float = 2.0,\n backoff_factor: float = 0.5,\n growth_interval: int = 2000,\n) -> Tuple[float, int]:\n # 请在此处编写你的实现代码\n pass\n", | |
| "reference_code": "import torch\nimport torch.nn as nn\nimport torch.nn.functional as F\nimport math\nfrom typing import List, Tuple, Dict, Optional\n\ndef dynamic_loss_scaler_update(scale: float, growth_tracker: int, found_inf: bool, growth_factor: float = 2.0, backoff_factor: float = 0.5, growth_interval: int = 2000) -> Tuple[float, int]:\n if found_inf:\n return scale * backoff_factor, 0\n tracker = growth_tracker + 1\n if tracker >= growth_interval:\n return scale * growth_factor, 0\n return scale, tracker\n", | |
| "test_code": "import torch\nimport torch.nn as nn\nimport torch.nn.functional as F\nimport math\nfrom typing import List, Tuple, Dict, Optional\n\ncases = [\n (\"遇到溢出立即减半并清零\", 65536.0, 1500, True, 2.0, 0.5, 2000),\n (\"正常步满间隔触发翻倍\", 65536.0, 1999, False, 2.0, 0.5, 2000),\n (\"正常步未满间隔\", 32768.0, 10, False, 2.0, 0.5, 2000),\n (\"小间隔测试\", 1024.0, 4, False, 2.0, 0.5, 5),\n]\nfor idx, (name, *args) in enumerate(cases, 1):\n t0 = time.time()\n try:\n exp = ref_obj(*args)\n act = user_obj(*args)\n match, err, max_diff = compare_outputs(act, exp, atol=atol, rtol=rtol)\n passed, msg = match, err\n except Exception as e:\n passed, msg = False, f\"Runtime Exception: {str(e)}\"\n act, exp, max_diff = None, None, None\n cases_results.append({\n \"case_id\": idx, \"case_name\": name, \"passed\": passed,\n \"time_ms\": round((time.time() - t0) * 1000, 2), \"message\": msg,\n \"user_output_summary\": summarize_obj(act) if act is not None else None,\n \"expected_summary\": summarize_obj(exp) if exp is not None else None,\n \"max_diff\": max_diff,\n })\n", | |
| "test_cases": [ | |
| { | |
| "name": "遇到溢出立即减半并清零", | |
| "input_desc": "真实参数契约用例 (scale, growth_tracker, found_inf, growth_factor, backoff_factor, growth_interval)", | |
| "params": [ | |
| { | |
| "name": "scale", | |
| "type": "float", | |
| "value": "65536.0" | |
| }, | |
| { | |
| "name": "growth_tracker", | |
| "type": "int", | |
| "value": "1500" | |
| }, | |
| { | |
| "name": "found_inf", | |
| "type": "bool", | |
| "value": "True" | |
| }, | |
| { | |
| "name": "growth_factor", | |
| "type": "float", | |
| "value": "2.0" | |
| }, | |
| { | |
| "name": "backoff_factor", | |
| "type": "float", | |
| "value": "0.5" | |
| }, | |
| { | |
| "name": "growth_interval", | |
| "type": "int", | |
| "value": "2000" | |
| } | |
| ], | |
| "raw_inputs": { | |
| "scale": "65536.0", | |
| "growth_tracker": "1500", | |
| "found_inf": "True", | |
| "growth_factor": "2.0", | |
| "backoff_factor": "0.5", | |
| "growth_interval": "2000" | |
| } | |
| }, | |
| { | |
| "name": "正常步满间隔触发翻倍", | |
| "input_desc": "真实参数契约用例 (scale, growth_tracker, found_inf, growth_factor, backoff_factor, growth_interval)", | |
| "params": [ | |
| { | |
| "name": "scale", | |
| "type": "float", | |
| "value": "65536.0" | |
| }, | |
| { | |
| "name": "growth_tracker", | |
| "type": "int", | |
| "value": "1999" | |
| }, | |
| { | |
| "name": "found_inf", | |
| "type": "bool", | |
| "value": "False" | |
| }, | |
| { | |
| "name": "growth_factor", | |
| "type": "float", | |
| "value": "2.0" | |
| }, | |
| { | |
| "name": "backoff_factor", | |
| "type": "float", | |
| "value": "0.5" | |
| }, | |
| { | |
| "name": "growth_interval", | |
| "type": "int", | |
| "value": "2000" | |
| } | |
| ], | |
| "raw_inputs": { | |
| "scale": "65536.0", | |
| "growth_tracker": "1999", | |
| "found_inf": "False", | |
| "growth_factor": "2.0", | |
| "backoff_factor": "0.5", | |
| "growth_interval": "2000" | |
| } | |
| }, | |
| { | |
| "name": "正常步未满间隔", | |
| "input_desc": "真实参数契约用例 (scale, growth_tracker, found_inf, growth_factor, backoff_factor, growth_interval)", | |
| "params": [ | |
| { | |
| "name": "scale", | |
| "type": "float", | |
| "value": "32768.0" | |
| }, | |
| { | |
| "name": "growth_tracker", | |
| "type": "int", | |
| "value": "10" | |
| }, | |
| { | |
| "name": "found_inf", | |
| "type": "bool", | |
| "value": "False" | |
| }, | |
| { | |
| "name": "growth_factor", | |
| "type": "float", | |
| "value": "2.0" | |
| }, | |
| { | |
| "name": "backoff_factor", | |
| "type": "float", | |
| "value": "0.5" | |
| }, | |
| { | |
| "name": "growth_interval", | |
| "type": "int", | |
| "value": "2000" | |
| } | |
| ], | |
| "raw_inputs": { | |
| "scale": "32768.0", | |
| "growth_tracker": "10", | |
| "found_inf": "False", | |
| "growth_factor": "2.0", | |
| "backoff_factor": "0.5", | |
| "growth_interval": "2000" | |
| } | |
| } | |
| ], | |
| "time_limit": 5.0, | |
| "memory_limit_mb": 512, | |
| "atol": 0.0001, | |
| "rtol": 0.0001, | |
| "forbidden_modules": [ | |
| "os", | |
| "sys", | |
| "subprocess", | |
| "socket", | |
| "shutil", | |
| "pathlib", | |
| "ctypes", | |
| "multiprocessing" | |
| ], | |
| "forbidden_calls": [ | |
| "eval", | |
| "exec", | |
| "compile", | |
| "open", | |
| "__import__", | |
| "input", | |
| "breakpoint" | |
| ], | |
| "is_featured": false, | |
| "editorial_md": "#### 💡 核心算法原理与数学推导\n**AMP 混合精度动态损失缩放器 (Dynamic Loss Scaler) 状态机**(`dynamic_loss_scaler_update`)聚焦于 **AIInfra** 中的关键计算逻辑:\n\n实现 `torch.cuda.amp.GradScaler` 的动态缩放系数状态更新:若本步梯度出现 `Inf/NaN`(`found_inf=True`),则将 `scale` 乘以 `backoff_factor` 并把连续正常步数计数器 `growth_tracker` 归零;若 `found_inf=False`,则将 `growth_tracker` 加 1,若此时 `growth_tracker == growth_interval`,则将 `scale` 乘以 `growth_factor` 并把 `growth_tracker` 归零。返回 `(new_scale, new_tracker)`。\n\n在实现中,函数接收参数 `scale` (`float`)、`growth_tracker` (`int`)、`found_inf` (`bool`)、`growth_factor` (`float`)、`backoff_factor` (`float`)、`growth_interval` (`int`),目标是计算出类型与形状严格符合 `Tuple[float, int]` 契约的结果。\n\n#### 📐 输入输出张量变换与步骤拆解\n- **输入基准规格**:`scale` 规格为 `float` (示例值: `65536.0`),`growth_tracker` 规格为 `int` (示例值: `1500`),`found_inf` 规格为 `bool` (示例值: `True`),`growth_factor` 规格为 `float` (示例值: `2.0`),`backoff_factor` 规格为 `float` (示例值: `0.5`),`growth_interval` 规格为 `int` (示例值: `2000`)。\n- **期望输出规格**:`Tuple` (`float` (示例值: `32768.0`), `int` (示例值: `0`))。\n\n**具体推导与计算步骤如下:**\n1. 条件分支判断 `if found_inf`:处理边界条件、掩码过滤或特殊退化情形。\n2. 计算中间量 `tracker`:通过表达式 `growth_tracker + 1` 完成张量或数值变换。\n3. 条件分支判断 `if tracker >= growth_interval`:处理边界条件、掩码过滤或特殊退化情形。\n4. 返回最终结果:输出 `scale, tracker`,严格对齐函数签名要求的返回类型与维度。\n\n#### 📊 复杂度分析\n- **时间复杂度**:$O(1)$,仅涉及标量代数运算或闭式公式求值,执行耗时与规模参数大小无关。\n- **空间复杂度**:$O(1)$,仅使用常数级标量局部变量,无额外张量显存分配。\n\n#### ⚠️ 关键实现细节与易错点\n- **输入输出类型与形状契约**:请严格按照函数签名返回对应类型(`torch.Tensor`、`Tuple` 或标量数值),避免改变张量维度。\n- **边界输入鲁棒性**:注意单样本(`Batch=1`)、零值输入或极端超参数下的计算行为,禁止硬编码特定测试用例常量。\n- **向量化算子优先**:优先使用 PyTorch 原生向量化张量算子,避免低效的逐元素循环。\n\n#### 💻 官方标准参考实现\n```python\nimport torch\nimport torch.nn as nn\nimport torch.nn.functional as F\nimport math\nfrom typing import List, Tuple, Dict, Optional\n\ndef dynamic_loss_scaler_update(scale: float, growth_tracker: int, found_inf: bool, growth_factor: float = 2.0, backoff_factor: float = 0.5, growth_interval: int = 2000) -> Tuple[float, int]:\n if found_inf:\n return scale * backoff_factor, 0\n tracker = growth_tracker + 1\n if tracker >= growth_interval:\n return scale * growth_factor, 0\n return scale, tracker\n```" | |
| } |