codestar-backend / data /problems /batchnorm1d-inference.json
github-actions
Sync backend from GitHub 47e8067
5f7afef
Raw History Blame Contribute Delete
15 kB
{
"slug": "batchnorm1d-inference",
"title": "手撕 1D 批归一化推理步 (BatchNorm1d Inference)",
"difficulty": "Easy",
"category": "DL",
"tags": [
"常见手撕",
"BatchNorm",
"推理加速"
],
"description_md": "### 题目描述\n使用预先保存的 running_mean 与 running_var 执行推理阶段归一化:$\\displaystyle \\frac{x - \\mu}{\\sqrt{\\sigma^2 + \\epsilon}} \\gamma + \\beta$。\n\n### 函数签名\n```python\ndef batchnorm1d_infer(\n x: torch.Tensor,\n running_mean: torch.Tensor,\n running_var: torch.Tensor,\n gamma: torch.Tensor,\n beta: torch.Tensor,\n eps: float = 1e-5,\n) -> torch.Tensor:\n```\n\n### 示例\n```python\n# 输入\nx = [[1.0, 2.0], [3.0, 4.0]]\nrunning_mean = [1.0, 2.0]\nrunning_var = [1.0, 4.0]\ngamma = [1.0, 2.0]\nbeta = [0.5, -0.5]\neps = 1e-05\n\n# 输出 (Tensor, shape=[2, 2], dtype=float32)\n[[0.5, -0.5], [2.5, 1.5]]\n```\n\n### 参数规格与约束\n- **`x`**:`torch.Tensor`,测试规格 `shape=[4, 8], dtype=float32`\n- **`running_mean`**:`torch.Tensor`,测试规格 `shape=[8], dtype=float32`\n- **`running_var`**:`torch.Tensor`,测试规格 `shape=[8], dtype=float32`\n- **`gamma`**:`torch.Tensor`,测试规格 `shape=[8], dtype=float32`\n- **`beta`**:`torch.Tensor`,测试规格 `shape=[8], dtype=float32`\n- **`eps`**:`float`(默认 `1e-5`)\n- **返回值**:`torch.Tensor`,输出规格 `shape=[4, 8], dtype=float32`\n- **精度容差**:`atol=0.0001, rtol=0.0001`",
"entry_point": "batchnorm1d_infer",
"starter_code": "import torch\nimport torch.nn as nn\nimport torch.nn.functional as F\nimport math\nfrom typing import Optional, List, Tuple\n\ndef batchnorm1d_infer(\n x: torch.Tensor,\n running_mean: torch.Tensor,\n running_var: torch.Tensor,\n gamma: torch.Tensor,\n beta: torch.Tensor,\n eps: float = 1e-5,\n) -> torch.Tensor:\n \"\"\"\n 手撕 1D 批归一化推理步 (BatchNorm1d Inference)\n \"\"\"\n # 请在此编写代码\n pass\n",
"reference_code": "import torch\nimport torch.nn as nn\nimport torch.nn.functional as F\nimport math\nfrom typing import Optional, List, Tuple\n\ndef batchnorm1d_infer(x: torch.Tensor, running_mean: torch.Tensor, running_var: torch.Tensor, gamma: torch.Tensor, beta: torch.Tensor, eps: float = 1e-5) -> torch.Tensor:\n return ((x - running_mean) / torch.sqrt(running_var + eps)) * gamma + beta\n",
"forbidden_modules": [
"os",
"sys",
"subprocess",
"shutil",
"socket",
"requests"
],
"forbidden_calls": [],
"time_limit": 3.0,
"memory_limit_mb": 512,
"atol": 0.0001,
"rtol": 0.0001,
"test_code": "import torch\nimport torch.nn as nn\nimport torch.nn.functional as F\nimport math\n\ncases = [\n ('BN推理', torch.tensor([[1.9269, 1.4873, 0.9007, -2.1055, 0.6784, -1.2345, -0.0431, -1.6047], [-0.7521, 1.6487, -0.3925, -1.4036, -0.7279, -0.5594, -0.7688, 0.7624], [1.6423, -0.1596, -0.4974, 0.4396, -0.7581, 1.0783, 0.8008, 1.6806], [1.2791, 1.2964, 0.6105, 1.3347, -0.2316, 0.0418, -0.2516, 0.8599]], dtype=torch.float32), torch.tensor([0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], dtype=torch.float32), torch.tensor([1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0], dtype=torch.float32), torch.tensor([1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0], dtype=torch.float32), torch.tensor([0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], dtype=torch.float32), 1e-05),\n ('数值缩放与幅值扰动验证', torch.tensor([[2.8904, 2.2309, 1.3511, -3.1583, 1.0176, -1.8518, -0.0646, -2.407], [-1.1282, 2.4731, -0.5887, -2.1054, -1.0918, -0.8391, -1.1533, 1.1437], [2.4635, -0.2394, -0.7461, 0.6594, -1.1372, 1.6175, 1.2012, 2.5209], [1.9187, 1.9446, 0.9157, 2.0021, -0.3474, 0.0626, -0.3774, 1.2898]], dtype=torch.float32), torch.tensor([0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], dtype=torch.float32), torch.tensor([1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0], dtype=torch.float32), torch.tensor([1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0], dtype=torch.float32), torch.tensor([0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], dtype=torch.float32), 8e-06),\n ('多维批次与参数泛化验证', torch.tensor([[0.9635, 0.7436, 0.4504, -1.0528, 0.3392, -0.6173, -0.0215, -0.8023], [-0.3761, 0.8244, -0.1962, -0.7018, -0.3639, -0.2797, -0.3844, 0.3812], [0.8212, -0.0798, -0.2487, 0.2198, -0.3791, 0.5392, 0.4004, 0.8403], [0.6396, 0.6482, 0.3052, 0.6674, -0.1158, 0.0209, -0.1258, 0.4299]], dtype=torch.float32), torch.tensor([0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], dtype=torch.float32), torch.tensor([1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0], dtype=torch.float32), torch.tensor([1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0], dtype=torch.float32), torch.tensor([0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], dtype=torch.float32), 1.2e-05),\n ('边界极值与稳定性验证', torch.tensor([[4.2392, 3.272, 1.9816, -4.6321, 1.4925, -2.716, -0.0947, -3.5303], [-1.6547, 3.6272, -0.8635, -3.0879, -1.6013, -1.2307, -1.6914, 1.6774], [3.6131, -0.3511, -1.0943, 0.9671, -1.6679, 2.3723, 1.7618, 3.6974], [2.8141, 2.8521, 1.343, 2.9364, -0.5096, 0.0919, -0.5535, 1.8917]], dtype=torch.float32), torch.tensor([0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], dtype=torch.float32), torch.tensor([1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0], dtype=torch.float32), torch.tensor([1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0], dtype=torch.float32), torch.tensor([0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], dtype=torch.float32), 5e-06),\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",
"is_featured": true,
"editorial_md": "#### 💡 核心算法原理与数学推导\n**手撕 1D 批归一化推理步 (BatchNorm1d Inference)**(`batchnorm1d_infer`)聚焦于 **DL** 中的关键计算逻辑:\n\n使用预先保存的 running_mean 与 running_var 执行推理阶段归一化:$\\displaystyle \\frac{x - \\mu}{\\sqrt{\\sigma^2 + \\epsilon}} \\gamma + \\beta$。\n\n在实现中,函数接收参数 `x` (`torch.Tensor`)、`running_mean` (`torch.Tensor`)、`running_var` (`torch.Tensor`)、`gamma` (`torch.Tensor`)、`beta` (`torch.Tensor`)、`eps` (`float`),目标是计算出类型与形状严格符合 `torch.Tensor` 契约的结果。\n\n#### 📐 输入输出张量变换与步骤拆解\n- **输入基准规格**:`x` 规格为 `torch.Tensor(shape=[4, 8], dtype=float32)`,`running_mean` 规格为 `torch.Tensor(shape=[8], dtype=float32)`,`running_var` 规格为 `torch.Tensor(shape=[8], dtype=float32)`,`gamma` 规格为 `torch.Tensor(shape=[8], dtype=float32)`,`beta` 规格为 `torch.Tensor(shape=[8], dtype=float32)`,`eps` 规格为 `float` (示例值: `1e-05`)。\n- **期望输出规格**:`torch.Tensor(shape=[4, 8], dtype=float32)`。\n\n**具体推导与计算步骤如下:**\n1. 返回最终结果:输出 `((x - running_mean) / torch.sqrt(running_var + eps)) * gamma + beta`,严格对齐函数签名要求的返回类型与维度。\n\n#### 📊 复杂度分析\n- **时间复杂度**:$O(N)$(线性于输入张量总元素个数 $\\prod d_i$),所有运算均为向量化逐元素算子或沿指定维度的归约计算。\n- **空间复杂度**:$O(N)$,需要与输入或输出张量同规模的显存空间保存计算结果。\n\n#### ⚠️ 关键实现细节与易错点\n- **除零保护与数值平滑 (`eps`)**:在执行分母归一化、标准差缩放或对数运算时,必须按公式位置准确加上 `eps`(例如在平方根内 `sqrt(var + eps)` 还是分母外 `+ eps`),防止全零或常量输入触发 `NaN` / `Inf`。\n- **边界输入鲁棒性**:注意单样本(`Batch=1`)、单步序列(`Seq=1`)、零值输入或极端超参数下的计算行为,不要在代码中硬编码特定测试用例的维度常数。\n- **纯向量化算子优先**:尽量使用 PyTorch 原生向量化张量算子替代低效的 Python 逐元素循环,以兼顾代码简洁性与执行性能。\n\n#### 💻 官方标准参考实现\n```python\nimport torch\nimport torch.nn as nn\nimport torch.nn.functional as F\nimport math\nfrom typing import Optional, List, Tuple\n\ndef batchnorm1d_infer(x: torch.Tensor, running_mean: torch.Tensor, running_var: torch.Tensor, gamma: torch.Tensor, beta: torch.Tensor, eps: float = 1e-5) -> torch.Tensor:\n return ((x - running_mean) / torch.sqrt(running_var + eps)) * gamma + beta\n```",
"difficulty_breakdown": {
"concept": 1,
"implementation": 1,
"math_derivation": 2,
"boundary_complexity": 2
},
"test_cases": [
{
"name": "BN推理",
"input_desc": "真实参数契约用例 (x, running_mean, running_var, gamma, beta, eps)",
"params": [
{
"name": "x",
"type": "torch.Tensor",
"value": "torch.tensor([[1.0, 2.0], [3.0, 4.0]], dtype=torch.float32)"
},
{
"name": "running_mean",
"type": "torch.Tensor",
"value": "torch.tensor([1.0, 2.0], dtype=torch.float32)"
},
{
"name": "running_var",
"type": "torch.Tensor",
"value": "torch.tensor([1.0, 4.0], dtype=torch.float32)"
},
{
"name": "gamma",
"type": "torch.Tensor",
"value": "torch.tensor([1.0, 2.0], dtype=torch.float32)"
},
{
"name": "beta",
"type": "torch.Tensor",
"value": "torch.tensor([0.5, -0.5], dtype=torch.float32)"
},
{
"name": "eps",
"type": "float",
"value": "1e-05"
}
],
"raw_inputs": {
"x": "torch.tensor([[1.0, 2.0], [3.0, 4.0]], dtype=torch.float32)",
"running_mean": "torch.tensor([1.0, 2.0], dtype=torch.float32)",
"running_var": "torch.tensor([1.0, 4.0], dtype=torch.float32)",
"gamma": "torch.tensor([1.0, 2.0], dtype=torch.float32)",
"beta": "torch.tensor([0.5, -0.5], dtype=torch.float32)",
"eps": "1e-05"
}
},
{
"name": "数值缩放与幅值扰动验证",
"input_desc": "真实参数契约用例 (x, running_mean, running_var, gamma, beta, eps)",
"params": [
{
"name": "x",
"type": "torch.Tensor",
"value": "torch.tensor([[2.8904, 2.2309, 1.3511, -3.1583, 1.0176, -1.8518, -0.0646, -2.407], [-1.1282, 2.4731, -0.5887, -2.1054, -1.0918, -0.8391, -1.1533, 1.1437], [2.4635, -0.2394, -0.7461, 0.6594, -1.1372, 1.6175, 1.2012, 2.5209], [1.9187, 1.9446, 0.9157, 2.0021, -0.3474, 0.0626, -0.3774, 1.2898]], dtype=torch.float32)"
},
{
"name": "running_mean",
"type": "torch.Tensor",
"value": "torch.tensor([0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], dtype=torch.float32)"
},
{
"name": "running_var",
"type": "torch.Tensor",
"value": "torch.tensor([1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0], dtype=torch.float32)"
},
{
"name": "gamma",
"type": "torch.Tensor",
"value": "torch.tensor([1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0], dtype=torch.float32)"
},
{
"name": "beta",
"type": "torch.Tensor",
"value": "torch.tensor([0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], dtype=torch.float32)"
},
{
"name": "eps",
"type": "float",
"value": "8e-06"
}
],
"raw_inputs": {
"x": "torch.tensor([[2.8904, 2.2309, 1.3511, -3.1583, 1.0176, -1.8518, -0.0646, -2.407], [-1.1282, 2.4731, -0.5887, -2.1054, -1.0918, -0.8391, -1.1533, 1.1437], [2.4635, -0.2394, -0.7461, 0.6594, -1.1372, 1.6175, 1.2012, 2.5209], [1.9187, 1.9446, 0.9157, 2.0021, -0.3474, 0.0626, -0.3774, 1.2898]], dtype=torch.float32)",
"running_mean": "torch.tensor([0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], dtype=torch.float32)",
"running_var": "torch.tensor([1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0], dtype=torch.float32)",
"gamma": "torch.tensor([1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0], dtype=torch.float32)",
"beta": "torch.tensor([0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], dtype=torch.float32)",
"eps": "8e-06"
}
},
{
"name": "多维批次与参数泛化验证",
"input_desc": "真实参数契约用例 (x, running_mean, running_var, gamma, beta, eps)",
"params": [
{
"name": "x",
"type": "torch.Tensor",
"value": "torch.tensor([[0.9635, 0.7436, 0.4504, -1.0528, 0.3392, -0.6173, -0.0215, -0.8023], [-0.3761, 0.8244, -0.1962, -0.7018, -0.3639, -0.2797, -0.3844, 0.3812], [0.8212, -0.0798, -0.2487, 0.2198, -0.3791, 0.5392, 0.4004, 0.8403], [0.6396, 0.6482, 0.3052, 0.6674, -0.1158, 0.0209, -0.1258, 0.4299]], dtype=torch.float32)"
},
{
"name": "running_mean",
"type": "torch.Tensor",
"value": "torch.tensor([0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], dtype=torch.float32)"
},
{
"name": "running_var",
"type": "torch.Tensor",
"value": "torch.tensor([1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0], dtype=torch.float32)"
},
{
"name": "gamma",
"type": "torch.Tensor",
"value": "torch.tensor([1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0], dtype=torch.float32)"
},
{
"name": "beta",
"type": "torch.Tensor",
"value": "torch.tensor([0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], dtype=torch.float32)"
},
{
"name": "eps",
"type": "float",
"value": "1.2e-05"
}
],
"raw_inputs": {
"x": "torch.tensor([[0.9635, 0.7436, 0.4504, -1.0528, 0.3392, -0.6173, -0.0215, -0.8023], [-0.3761, 0.8244, -0.1962, -0.7018, -0.3639, -0.2797, -0.3844, 0.3812], [0.8212, -0.0798, -0.2487, 0.2198, -0.3791, 0.5392, 0.4004, 0.8403], [0.6396, 0.6482, 0.3052, 0.6674, -0.1158, 0.0209, -0.1258, 0.4299]], dtype=torch.float32)",
"running_mean": "torch.tensor([0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], dtype=torch.float32)",
"running_var": "torch.tensor([1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0], dtype=torch.float32)",
"gamma": "torch.tensor([1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0], dtype=torch.float32)",
"beta": "torch.tensor([0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], dtype=torch.float32)",
"eps": "1.2e-05"
}
}
]
}