-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathstep_05.py
More file actions
67 lines (52 loc) · 2.2 KB
/
step_05.py
File metadata and controls
67 lines (52 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# ===----------------------------------------------------------------------=== #
#
# This file is Modular Inc proprietary.
#
# ===----------------------------------------------------------------------=== #
"""
Step 05: Layer Normalization
Implement layer normalization that normalizes activations for training stability.
Tasks:
1. Import functional module (as F) and Tensor from max.nn
2. Initialize learnable weight (gamma) and bias (beta) parameters
3. Apply layer normalization using F.layer_norm in the forward pass
Run: pixi run s05
"""
# 1: Import the required modules from MAX
# TODO: Import functional module max.functional with the alias F
# https://docs.modular.com/max/api/python/functional/
# TODO: Import Tensor from max.tensor
# https://docs.modular.com/max/api/python/tensor/
from max.graph import DimLike
from max.nn import Module
from max.tensor import Tensor
class LayerNorm(Module):
"""Layer normalization module.
Args:
dim: Dimension to normalize over.
eps: Epsilon for numerical stability.
"""
def __init__(self, dim: DimLike, *, eps: float = 1e-5) -> None:
super().__init__()
self.eps = eps
# 2: Initialize learnable weight and bias parameters
# TODO: Create self.weight as a Tensor of ones with shape [dim]
# https://docs.modular.com/max/api/python/tensor/#max.tensor.Tensor.ones
# Hint: This is the gamma parameter in layer normalization
self.weight = None
# TODO: Create self.bias as a Tensor of zeros with shape [dim]
# https://docs.modular.com/max/api/python/tensor/#max.tensor.Tensor.zeros
# Hint: This is the beta parameter in layer normalization
self.bias = None
def forward(self, x: Tensor) -> Tensor:
"""Apply layer normalization.
Args:
x: Input tensor.
Returns:
Normalized tensor.
"""
# 3: Apply layer normalization and return the result
# TODO: Use F.layer_norm() with x, gamma=self.weight, beta=self.bias, epsilon=self.eps
# https://docs.modular.com/max/api/python/functional/#max.functional.layer_norm
# Hint: Layer normalization normalizes across the last dimension
return None