Logo Migrate Max V2 To V3

Migrate Max V2 To V3

Tổ chức
modular
migrate-max-v2-to-v3

Use when migrating a MAX model from the V2 graph API (max.nn, TensorValue, explicit Graph construction) to ModuleV3 (max.experimental.nn, Tensor, F.lazy() + compile()). Triggers on: "migrate to ModuleV3", "port a max.nn model to max.experimental", "convert TensorValue to Tensor", "update my model to the V3 API", "ModuleV3 migration". Workflow: read the concept map, rewrite imports and __call__ signatures, and replace graph building with compile(weights=...). Align weight names with the new module hierarchy and move the KV cache unflatten into forward(). Port sharding to a DeviceMesh when the V2 model is sharded, then verify greedy outputs match the V2 implementation.

Add to TypingMind

Tổng quan

Nhà phát hànhmodular
Kho mãskills
Tên kỹ năngmigrate-max-v2-to-v3
Sao
204
Bản fork
21
Tệp đi kèm
1
Liên kết
  • Hướng dẫn bằng Markdown

    Tệp SKILL.md được mô hình tải khi cần, nên chỉ tốn token khi có yêu cầu thực sự phù hợp.

  • Dùng được với mọi LLM

    Kỹ năng AI dùng hướng dẫn viết bằng Markdown thay vì mã dành riêng cho một nhà cung cấp, nên dùng được với GPT, Claude, Gemini, Grok hoặc mô hình cục bộ.

  • 1 tệp đi kèm

    Các tập lệnh, mẫu và tài liệu tham khảo mà mô hình có thể đọc khi làm việc. Các tệp chỉ cho phép đọc và không bao giờ được thực thi.

  • Mã nguồn mở

    Được modular công bố trên GitHub. Hãy đọc các tệp nguồn trước khi cài đặt.

Cài đặt

Cài đặt kỹ năng AI Migrate Max V2 To V3 trong TypingMind để dùng với bất kỳ LLM nào, hoặc thêm vào một tác nhân AI khác có thể đọc SKILL.md.

1

Cài đặt trong TypingMind

TypingMind cài đặt kỹ năng trực tiếp từ thư mục GitHub — đọc SKILL.md, đóng gói các tệp tài nguyên và lưu kết quả trên máy.

  1. Mở ứng dụng và vào Plugin → Kỹ năng.
  2. Chọn "Cài đặt từ GitHub".
  3. Dán URL của thư mục kỹ năng được hiển thị bên dưới rồi xác nhận.
    https://github.com/modular/skills/tree/main/migrate-max-v2-to-v3
  4. Bật kỹ năng trong từng cuộc trò chuyện mà bạn muốn sử dụng kỹ năng đó.
Plugin → Kỹ năng → Thêm kỹ năng → Từ URL GitHub, sau đó dán URL thư mục và nhấn Tiếp tục.
2

Cài đặt trong tác nhân AI khác

Bất kỳ tác nhân AI nào đọc được định dạng Agent Skills đều có thể dùng kỹ năng này — hãy sao chép thư mục vào thư mục kỹ năng của tác nhân AI đó.

Claude Code — .claude/skills
git clone --depth 1 https://github.com/modular/skills.git /tmp/skills
mkdir -p .claude/skills
cp -r /tmp/skills/migrate-max-v2-to-v3 .claude/skills/migrate-max-v2-to-v3
Khởi động lại Claude Code sau khi sao chép để ứng dụng nhận kỹ năng mới.

Sử dụng trong TypingMind

Bật Migrate Max V2 To V3 trong bất kỳ cuộc trò chuyện nào trên TypingMind, mô hình sẽ tự xử lý phần còn lại. Tên và mô tả của kỹ năng nằm trong prompt hệ thống. Ngay khi có yêu cầu phù hợp, mô hình tự tải toàn bộ hướng dẫn — bạn không cần gọi thủ công và kỹ năng không tốn token cho đến khi thực sự được dùng.

Mô hình tự nạp Migrate Max V2 To V3 ngay khi có yêu cầu phù hợp.

Dùng được với mọi mô hình AI

Kỹ năng AI là các hướng dẫn Markdown thuần túy, không phải mã dành riêng cho một nhà cung cấp, nên bạn có thể dùng Migrate Max V2 To V3 với cả những mô hình khác ngoài mô hình ban đầu. Chỉ cần cài đặt một lần trong TypingMind rồi dùng với GPT-5, Claude, Gemini, Grok, DeepSeek, Mistral, Llama hoặc mô hình cục bộ do bạn tự chạy — tất cả đều dùng khóa API của riêng bạn.

  • Chỉ tải khi cần

    Prompt hệ thống chỉ chứa tên và mô tả. Hướng dẫn được tải khi có yêu cầu phù hợp đầu tiên, nên khi kỹ năng chưa được dùng, bạn không tốn token cho phần hướng dẫn.

  • Đổi mô hình ngay trong cuộc trò chuyện

    Vì kỹ năng là hướng dẫn chứ không phải mã, việc đổi mô hình không làm kỹ năng ngừng hoạt động — mô hình tiếp theo vẫn đọc cùng tệp SKILL.md.

Hướng dẫn của kỹ năng

Đây là nội dung SKILL.md mà mô hình tải. Hãy đọc trước khi cài đặt — kỹ năng là những hướng dẫn mà mô hình của bạn sẽ làm theo.

Migrate a MAX model from V2 to ModuleV3

Input: the V2 model source to migrate ($ARGUMENTS).

ModuleV3 (max.experimental.nn) is the current MAX model API. A V2 model builds an explicit Graph over TensorValue values with layers from max.nn. A ModuleV3 model declares a forward() over Tensor values and compiles with F.lazy() + compile(). Read references/v2-v3-basics.md first. It covers F.lazy(), compile(weights=...), dtype handling with auto_cast, forward() vs __call__(), the distributed mapping, and the import rules.

Workflow

1. Confirm the model is V2

Classify by imports, and read the file when grep and the imports disagree. V2 code imports its layers from max.nn and builds an explicit Graph over TensorValue. ModuleV3 code imports max.experimental.nn and subclasses its Module. Scoped greps shortcut the read:

bash
grep -rn "max.experimental" <model-path>
grep -rEn "^\s*(from|import) max\.nn" <model-path>

Hits from the first mean the model is already (at least partly) V3 and needs review, not migration. Hits from the second mean V2. Imports from max.graph alone don't decide: V3 code keeps TensorType and DeviceRef from max.graph for input specs. A file that hits both is mixed. Read it before migrating anything.

2. Keep the V2 code, add the V3 variant

Leave the V2 implementation untouched so it keeps working while the V3 variant is under construction. Put the V3 code alongside it: a <name>_modulev3 directory when migrating a registered architecture, or a new module file when the model lives in your own package. When migrating a registered architecture, name the V3 variant with the _ModuleV3 suffix. max serve --prefer-module-v3 selects it while the V2 name keeps serving by default.

3. Rewrite imports

python
# REMOVE:
from max.graph import Graph, TensorValue, BufferValue, ops
from max.nn.layer import Module, LayerList
from max.nn.linear import Linear
from max.nn.norm.rms_norm import RMSNorm

# ADD:
from max.experimental import functional as F
from max.experimental.tensor import Tensor
from max.experimental.nn import Module
from max.experimental.nn.linear import Linear
from max.experimental.nn.norm import RMSNorm
from max.experimental.nn.sequential import ModuleList

# KEEP (TensorValue-free, shared with V2):
from max.nn.kv_cache import KVCacheParams, KVCacheParamInterface
from max.nn.attention import MHAMaskVariant
  • TensorType and DeviceRef stay from max.graph. They define input specs, not graph values.
  • max.nn imports are fine when the imported class doesn't use TensorValue.
  • PagedCacheValues comes from max.experimental.nn.common_layers.kv_cache for the V3 type. max.nn exports a PagedCacheValues of its own, an alias for the V2 input type without from_upstream(). Import from the max.experimental path to get the V3 type.

4. Rewrite the module surface

  • __call__() becomes forward().
  • TensorValue and BufferValue become Tensor.
  • Module takes the call signature: class MyModel(Module[[Tensor, ...], tuple[Tensor, ...]]). Prefer specific types, and use ... only when forward() has *args.
  • Layer constructors take keyword args and drop dtype= and device=. V3 uses default dtype and device contexts, and model.to(device) moves the module to one device after construction.
  • LayerList becomes ModuleList from max.experimental.nn.sequential. ModuleList subclasses list, so it takes a single iterable: ModuleList(layers). ModuleList(*layers) raises TypeError.
  • Parameters are plain Tensor objects. Create them with Tensor.zeros(), max.experimental.random.*, and similar factories.

5. Rewrite operations

ops.* becomes F.* (ops.gather() → F.gather()). Most shape ops are methods on Tensor: reshape(), transpose(), permute(), squeeze(), unsqueeze(), split(), cast(). flatten() is the exception. It's functional-only (F.flatten(tensor, start_dim)).

6. Replace graph building with compile()

V2:

python
nn_model = MyModel(config)
nn_model.load_state_dict(state_dict)

with Graph("my_model", input_types=input_types) as graph:
    outputs = nn_model(*graph.inputs)
    graph.output(*outputs)

model = session.load(graph, weights_registry=state_dict)

V3:

python
with F.lazy():
    nn_model = MyModel(config)
    nn_model.to(device)

model = nn_model.compile(*input_types, weights=state_dict)
  • F.lazy() wraps construction and records weight tensors symbolically. The checkpoint passed to compile(weights=...) replaces them.
  • Every parameter needs a state-dict entry of matching shape and dtype: a missing one raises KeyError and a mismatched one ValueError. An entry that matches no parameter is left out without an error, so diff the state-dict keys against parameters after migrating (step 7).
  • A dtype mismatch between a parameter and its loaded tensor raises by default. Pass auto_cast=True to permit safe dtype casts.
  • Call the compiled model directly (model(tokens, ...)). Compiled outputs are Tensor objects. Code that consumed Buffer outputs reads the buffer via .driver_tensor.

7. Align weight names with the compiled hierarchy

compile(weights=...) resolves names from the root compiled module's attribute hierarchy. When migration introduces or renames wrapper attributes, checkpoint key mappings must follow: a root module storing self.language_model = ... needs every inner weight prefixed with language_model.. After migrating, check that every state-dict key is a valid attribute path from the root module.

Tied embeddings need explicit handling: conditionally create lm_head, and multiply by embed_tokens.weight.T in forward() when tie_word_embeddings is set. Keep one copy of the shared weight in the state dict.

8. Move the KV cache unflatten into forward()

The KV cache parameter types stay in max.nn. The values type has a V3 home in max.experimental.nn.common_layers.kv_cache. Use the methods on kv_params plus PagedCacheValues.from_upstream():

python
def forward(self, tokens: Tensor, ..., *variadic_args: Tensor):
    kv_inputs = iter(arg._graph_value for arg in variadic_args)
    symbolic_inputs = self.kv_params.unflatten_kv_inputs(kv_inputs)
    kv = PagedCacheValues.from_upstream(symbolic_inputs, tokens.mapping)

You need the ._graph_value extraction: variadic_args arrive as Tensor objects while the unflatten methods expect raw graph values. For sliding plus global attention, declare MultiKVCacheParams and use unflatten_basic_kv_tree(), wrapping each tree with its own PagedCacheValues.from_upstream().

9. Port sharding when the V2 model is sharded

V2 sharding sets a ShardingStrategy on each layer and calls .shard(devices) to get a per-device module list. V3 puts a DeviceMesh on the model and a placement mapping on each weight:

python
from max.experimental.nn.common_layers.linear import row_parallel
from max.experimental.sharding import DeviceMesh

model.mesh = DeviceMesh(devices, (n_devices,), ("tp",))
linear = row_parallel(Linear(in_dim, out_dim))

col_parallel() and row_parallel() set which mesh axis each weight dimension shards over, and V3 has no per-device module list. The V2 distributed machinery maps onto V3 as follows:

  • Allreduce and Signals: the sharding solver reads the tensors' placements and inserts the collective each op needs. For example, adding a row-parallel output (a Partial sum) to a replicated residual all-reduces it. Call F.allreduce_sum(), F.reduce_scatter(), or F.allgather() where the model controls the reduction. compile() adds the signal buffers the collectives use as graph inputs and supplies them at run time.
  • .shard() and per-device lists: construct the model inside default_device(mesh), and each weight is created on the mesh with the placement it declares. Module.to() moves a module to one device only and raises for a multi-device mesh. Per-device code runs through F.functional(), or through t.local_shards and Tensor.from_shard_values().
  • Expert parallelism: V3 reuses EPBatchManager. Its comm_buffers() returns the communication buffers as Tensor values, and the MoE layer passes each device's shard to the dispatch calls as a TensorValue. The pipeline model sets the manager up in _init_distributed_runtime() (deepseekV3_modulev3/model.py).

gemma3_modulev3/ (TP) and deepseekV3_modulev3/ (TP, TP + EP, DP + EP) show each pattern.

10. Verify against V2

Run both implementations on the same inputs and compare greedy tokens (and logits where convenient). Identical outputs confirm the migration. Run mypy over the migrated module. V3 relies on the Module[[...], ...] signatures, so type errors point to migration bugs.

Reference implementations

The registered architectures ship as readable Python source in the installed MAX package, under max/pipelines/architectures/. The V3 migrations to read:

  • Single-GPU V3: olmo3/, gpt_oss_modulev3/, llama3_modulev3/
  • Sharded V3: kimik2_5_modulev3/, gemma3_modulev3/, deepseekV3_modulev3/
  • Hybrid attention with recurrent state: nemotron_h_modulev3/
  • V2 for comparison: gpt_oss/, llama3/

When there is no Tensor equivalent

Every graph op and kernel is callable from V3: F.functional(fn) turns a function over TensorValue into one over Tensor. The options, and the wrapper pattern for bridging a per-device TensorValue helper into a Tensor API, are in references/v2-v3-basics.md.

Tệp đi kèm

Khi kỹ năng đã được tải, mô hình có thể đọc các tệp này nếu cần. Các tệp này chỉ dùng để đọc và không bao giờ được thực thi.

Câu hỏi thường gặp

Kỹ năng AI Migrate Max V2 To V3 làm được gì?

Use when migrating a MAX model from the V2 graph API (max.nn, TensorValue, explicit Graph construction) to ModuleV3 (max.experimental.nn, Tensor, F.lazy() + compile()). Triggers on: "migrate to ModuleV3", "port a max.nn model to max.experimental", "convert TensorValue to Tensor", "update my model to the V3 API", "ModuleV3 migration". Workflow: read the concept map, rewrite imports and __call__ signatures, and replace graph building with compile(weights=...). Align weight names with the new module hierarchy and move the KV cache unflatten into forward(). Port sharding to a DeviceMesh when th...

Vì sao nên dùng Migrate Max V2 To V3 trên TypingMind?

Vì bạn chỉ cần cài một lần rồi dùng với bất kỳ mô hình nào. Migrate Max V2 To V3 là nội dung Markdown chứ không phải mã dành riêng cho từng nhà cung cấp, nên cùng một kỹ năng có thể chạy trên GPT-5, Claude, Gemini, Grok hoặc mô hình trên thiết bị — và bạn có thể đổi mô hình ngay trong cuộc trò chuyện mà kỹ năng vẫn hoạt động. TypingMind dùng khóa API riêng của bạn, nên bạn thanh toán trực tiếp cho nhà cung cấp thay vì trả phí thuê bao theo suất người dùng, đồng thời kỹ năng và các cuộc trò chuyện vẫn nằm trong bộ lưu trữ của bạn.

Làm thế nào để cài Migrate Max V2 To V3 trong TypingMind?

Mở Plugin → Kỹ năng → Cài đặt từ GitHub trong TypingMind rồi dán https://github.com/modular/skills/tree/main/migrate-max-v2-to-v3. TypingMind đọc tệp SKILL.md, đóng gói các tệp đi kèm rồi cài đặt cả gói dưới dạng một kỹ năng mà bạn có thể bật cho từng cuộc trò chuyện.

Những mô hình AI nào có thể dùng Migrate Max V2 To V3?

Bất kỳ mô hình nào bạn kết nối trong TypingMind. Kỹ năng AI là các hướng dẫn bằng Markdown chứ không phải mã dành riêng cho từng nhà cung cấp, nên GPT, Claude, Gemini, Grok và các mô hình chạy trên thiết bị đều có thể tải kỹ năng này khi có yêu cầu phù hợp.

Có thể dùng Migrate Max V2 To V3 với bao nhiêu mô hình AI?

Bao nhiêu cũng được. Miễn là mô hình hỗ trợ kỹ năng, bạn có thể dùng Migrate Max V2 To V3 với mô hình đó — GPT, Claude, Gemini, Grok, DeepSeek, Mistral, Llama và nhiều mô hình khác — tất cả đều trên TypingMind với khóa API riêng của bạn.

Kỹ năng AI Migrate Max V2 To V3 có miễn phí không?

Kỹ năng này được modular phát hành trên GitHub. Hãy xem điều khoản giấy phép trong kho mã. Bạn chỉ trả tiền cho nhà cung cấp AI của mình theo số token đã dùng.

Kỹ năng AI là gì?

Kỹ năng AI là một gói hướng dẫn có thể tái sử dụng, chỉ cho mô hình AI cách thực hiện một tác vụ cụ thể. Kỹ năng tuân theo định dạng mở Agent Skills: một tệp SKILL.md chứa tên và mô tả, cùng các tập lệnh, mẫu hoặc tệp tham khảo mà mô hình có thể cần. Mô hình chỉ đọc hướng dẫn khi yêu cầu của bạn phù hợp với kỹ năng, nên kỹ năng đã cài đặt không phát sinh chi phí cho đến khi được sử dụng.

Kỹ năng AI khác gì với plugin hoặc máy chủ MCP?

Plugin hoặc máy chủ MCP cung cấp các công cụ mới mà mô hình có thể gọi. Khi mô hình gọi một công cụ, mã của công cụ đó chạy ở đâu đó và trả về kết quả. Còn kỹ năng AI cung cấp kiến thức và quy trình: cách tiếp cận tác vụ, các bước cần thực hiện và những tiêu chí mà kết quả cần đáp ứng. Kỹ năng chỉ là nội dung Markdown, nên không cần máy chủ, khóa API hay môi trường thực thi và hoạt động với mọi mô hình.

Xem tất cả

Tạo ngay không gian làm việc AI riêng

Đăng ký bản tin của chúng tôi để nhận thông báo về tính năng mới và các đợt tặng quà sắp tới 👇