Skip to content

Conversation

@Linorman
Copy link

@Linorman Linorman commented Oct 18, 2025

Description

Summary: Just fix param name in SentenceChunker to avoid bugs in runtime.

Fix: #(issue)

Docs Issue/PR: (docs-issue-or-pr-link)

Reviewer: @(reviewer)

Checklist:

  • I have performed a self-review of my own code | 我已自行检查了自己的代码
  • I have commented my code in hard-to-understand areas | 我已在难以理解的地方对代码进行了注释
  • I have added tests that prove my fix is effective or that my feature works | 我已添加测试以证明我的修复有效或功能正常
  • I have created related documentation issue/PR in MemOS-Docs (if applicable) | 我已在 MemOS-Docs 中创建了相关的文档 issue/PR(如果适用)
  • I have linked the issue to this PR (if applicable) | 我已将 issue 链接到此 PR(如果适用)
  • I have mentioned the person who will review this PR | 我已提及将审查此 PR 的人

@CaralHsi CaralHsi changed the base branch from main to dev November 4, 2025 08:25
@kakack
Copy link
Collaborator

kakack commented Nov 4, 2025

Please pull MemTensor:dev branch and solve the conflicts, thank you. @Linorman

Copilot AI review requested due to automatic review settings November 4, 2025 10:59
@Linorman
Copy link
Author

Linorman commented Nov 4, 2025

Please pull MemTensor:dev branch and solve the conflicts, thank you. @Linorman

Ok, I have already solved confilcts and merged to my main branch.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR updates the codebase to support both legacy and new API versions of the DynamicCache class from the transformers library. The new API uses a layers attribute with .keys and .values properties, while the legacy API uses key_cache and value_cache list attributes.

  • Removes version-based branching (previously using packaging.version checks) in favor of runtime attribute detection
  • Updates cache concatenation logic to handle both APIs through hasattr() checks
  • Fixes parameter naming in ChonkieSentenceChunker initialization

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
src/memos/memories/activation/kv.py Replaced version-based API detection with runtime attribute checking; removed unused imports; updated _concat_caches to mutate first cache in-place for new API
tests/memories/activation/test_kv.py Added compatibility layer in test helper make_filled_cache() and assertions to support both old and new DynamicCache APIs
src/memos/mem_os/utils/format_utils.py Updated serialization functions to detect and handle both DynamicCache API versions when extracting layer counts, device info, dtype, and tensor shapes
src/memos/chunkers/sentence_chunker.py Changed parameter name from tokenizer_or_token_counter to tokenizer to match the expected API

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +213 to +219
base = caches[0]
for layer in range(num_layers):
# gather all K and V for this layer
keys = [c.layers[layer].keys for c in caches]
vals = [c.layers[layer].values for c in caches]
# single concat per layer
merged.layers[layer].keys = torch.cat(keys, dim=-2)
merged.layers[layer].values = torch.cat(vals, dim=-2)

base.layers[layer].keys = torch.cat(keys, dim=-2)
base.layers[layer].values = torch.cat(vals, dim=-2)
return base
Copy link

Copilot AI Nov 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mutating the first cache object in-place (caches[0]) is problematic because it modifies the original cache that may still be referenced elsewhere. This could lead to unexpected side effects if the caller expects the original caches to remain unchanged. Consider creating a new DynamicCache() object and populating its layers similar to the legacy API path.

Copilot uses AI. Check for mistakes.
Comment on lines +1104 to +1109
t = getattr(lyr, "keys", None)
if t is None:
t = getattr(lyr, "values", None)
if t is not None:
device_str = str(t.device)
dtype_str = str(t.dtype)
Copy link

Copilot AI Nov 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The variable name t is unclear and could be improved for readability. Consider renaming it to tensor or sample_tensor to better convey its purpose as a tensor used to determine device and dtype information.

Suggested change
t = getattr(lyr, "keys", None)
if t is None:
t = getattr(lyr, "values", None)
if t is not None:
device_str = str(t.device)
dtype_str = str(t.dtype)
tensor = getattr(lyr, "keys", None)
if tensor is None:
tensor = getattr(lyr, "values", None)
if tensor is not None:
device_str = str(tensor.device)
dtype_str = str(tensor.dtype)

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants