Skip to content
Open

fix #15

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
*pth
.vscode
.gradio
visualization
visualization
.idea/
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions diganose.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# diagnose_import.py
import sys
import os

print("=== Python 路径诊断 ===")
print(f"Python 版本: {sys.version}")
print(f"Python 可执行文件: {sys.executable}")

print("\n=== PYTHONPATH 环境变量 ===")
print(f"PYTHONPATH: {os.environ.get('PYTHONPATH', '未设置')}")

print("\n=== sys.path 内容 ===")
for i, path in enumerate(sys.path):
print(f"{i:2d}: {path}")

print("\n=== 查找 models 模块 ===")
models_found = []
for path in sys.path:
models_path = os.path.join(path, "models")
if os.path.exists(models_path):
models_found.append(models_path)
print(f"✅ 找到: {models_path}")

if not models_found:
print("❌ 未找到 models 目录")

print("\n=== 尝试导入 ===")
try:
from models.curope import cuRoPE2D
print("✅ 导入成功!")
except ImportError as e:
print(f"❌ 导入失败: {e}")

# 更详细的错误信息
import traceback
traceback.print_exc()
29 changes: 29 additions & 0 deletions extern/CUT3R/src/croco/models/curope/my_build_cpp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import os
import torch
from torch.utils.cpp_extension import load

# 清理缓存
os.system('rm -rf /root/.cache/torch_extensions/py310_cu124/curope')

print("尝试只编译 curepe.cpp...")
try:
curepe_simple = load(
name='curope_simple',
sources=['curope.cpp'], # 只使用 cpp 文件
extra_cflags=['-O3', '-fPIC'],
extra_ldflags=['-Wl,-Bstatic', '-lstdc++', '-Wl,-Bdynamic', '-lpthread'],
verbose=True
)

print("简单版本中的函数:", [x for x in dir(curope_simple) if not x.startswith('__')])

# 测试 rope_2d 是否存在
if hasattr(curope_simple, 'rope_2d'):
print("找到 rope_2d 函数!")
func = curepe_simple.rope_2d
print(f"类型: {type(func)}")
if callable(func):
print(f"可调用,签名: {inspect.signature(func)}")

except Exception as e:
print(f"单独编译失败: {e}")
42 changes: 42 additions & 0 deletions extern/CUT3R/src/croco/models/curope/my_build_simple.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# build_simple.py
import torch
from torch.utils.cpp_extension import load
import os
import shutil



os.system('rm -rf /root/.cache/torch_extensions/py310_cu124/curope')

# 强制使用系统库路径
os.environ['LD_LIBRARY_PATH'] = '/usr/lib/x86_64-linux-gnu:' + os.environ.get('LD_LIBRARY_PATH', '')
os.environ['LIBRARY_PATH'] = '/usr/lib/x86_64-linux-gnu:' + os.environ.get('LIBRARY_PATH', '')

print("强制使用系统库编译...")

# 清理旧文件
for f in os.listdir('.'):
if f.endswith('.so'):
os.remove(f)

print("开始 JIT 编译...")
try:
curope = load(
name='curope',
sources=['curope.cpp', 'kernels.cu'],
extra_cflags=['-O3', '-fPIC'],
extra_cuda_cflags=[
'-O3',
'--ptxas-options=-v',
'--use_fast_math',
],
verbose=True,
with_cuda=True
)
print("编译成功!")
print("可用函数:", [x for x in dir(curope) if not x.startswith('_')])

except Exception as e:
print(f"编译失败: {e}")
import traceback
traceback.print_exc()
78 changes: 78 additions & 0 deletions extern/CUT3R/src/croco/models/curope/my_build_two.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import os
import sys
import torch

# 1. 从sys.modules中彻底移除curope模块
modules_to_remove = [name for name in sys.modules if 'curope' in name]
for name in modules_to_remove:
del sys.modules[name]

# 2. 清理所有可能的缓存目录
cache_paths = [
'/root/.cache/torch_extensions/py310_cu124/curope',
'/tmp/torch_extensions/py310_cu124/curope',
'./build',
'/root/.cache/torch_extensions/curope'
]

for path in cache_paths:
if os.path.exists(path):
os.system(f'rm -rf "{path}"')
print(f"已清理: {path}")

# 3. 强制垃圾回收
import gc
gc.collect()

print("缓存清理完成,重新编译模块...")

# 4. 重新编译
from torch.utils.cpp_extension import load

curope = load(
name='curope',
sources=['curope.cpp', 'kernels.cu'],
extra_cflags=['-O3', '-fPIC'],
extra_cuda_cflags=['-O3', '--ptxas-options=-v', '--use_fast_math'],
extra_ldflags=['-Wl,-Bstatic', '-lstdc++', '-Wl,-Bdynamic', '-lpthread'],
verbose=True
)

# 5. 立即检查
print("重新编译后的模块内容:", [x for x in dir(curope) if not x.startswith('__')])
print("模块文件:", getattr(curope, '__file__', '未知'))

# 6. 测试函数是否存在
if hasattr(curope, 'rope_2d'):
print("✓ 成功找到 rope_2d 函数!")
else:
print("✗ 仍然没有找到 rope_2d 函数")


# 立即验证并测试
print("编译后立即验证:")
print("模块内容:", [x for x in dir(curope) if not x.startswith('__')])

if hasattr(curope, 'rope_2d'):
print("✓ 找到 rope_2d,立即测试...")

# 立即测试
B, N, H, D = 1, 2, 1, 16
tokens = torch.randn(B, N, H, D * 4)
positions = torch.randint(0, 10, (B, N, 2), dtype=torch.int64)
original_tokens = tokens.clone()

print(f"原始 tokens 形状: {tokens.shape}")
print(f"原始 tokens 均值: {tokens.mean().item():.6f}")

try:
result = curope.rope_2d(tokens, positions, 10000.0, 1.0)
print(result)
# 检查输入张量是否被修改
print(f"修改后 tokens 均值: {tokens.mean().item():.6f}")
print(f"张量是否被修改: {not torch.allclose(original_tokens, tokens)}")
print(f"修改量均值: {(tokens - original_tokens).mean().item():.6f}")
except Exception as e:
print(f"✗ 测试失败: {e}")
else:
print("✗ 没有找到 rope_2d")
51 changes: 51 additions & 0 deletions extern/CUT3R/src/croco/models/curope/my_check_load.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import sys
import os

# 强制清理所有 curepe 相关模块
for key in list(sys.modules.keys()):
if 'curope' in key:
print(f"删除模块: {key}")
del sys.modules[key]

# 重新导入
import curope

print(f"实际加载的模块文件: {curope.__file__}")
print(f"模块所有属性: {[x for x in dir(curope) if not x.startswith('__')]}")

# 检查文件是否真的是我们替换的文件
expected_file = '/home/jhc/apps/anaconda3/envs/vmem/lib/python3.10/site-packages/curope.cpython-310-x86_64-linux-gnu.so'
if curope.__file__ == expected_file:
print("✓ 加载的是替换后的文件")
else:
print(f"✗ 加载的不是替换后的文件,而是: {curope.__file__}")


import hashlib

def compare_files():
"""比较两个文件的哈希值,确认替换是否成功"""

original = '/root/.cache/torch_extensions/py310_cu124/curope/curope.so'
replaced = '/home/jhc/apps/anaconda3/envs/vmem/lib/python3.10/site-packages/curope.cpython-310-x86_64-linux-gnu.so'

def file_hash(filepath):
with open(filepath, 'rb') as f:
return hashlib.md5(f.read()).hexdigest()

try:
hash_original = file_hash(original)
hash_replaced = file_hash(replaced)

print(f"原始文件哈希: {hash_original}")
print(f"替换文件哈希: {hash_replaced}")

if hash_original == hash_replaced:
print("✓ 文件替换成功,内容相同")
else:
print("✗ 文件内容不同,替换可能失败")

except Exception as e:
print(f"比较文件失败: {e}")

compare_files()
29 changes: 29 additions & 0 deletions extern/CUT3R/src/croco/models/curope/my_inspect_func.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# 详细检查函数信息
import inspect
import curope
import torch

def analyze_functions():
for func_name in ['cuRoPE2D', 'curope2d']:
if hasattr(curope, func_name):
func = getattr(curope, func_name)
print(f"\n=== {func_name} ===")
print(f"文档: {func.__doc__}")
try:
print(f"签名: {inspect.signature(func)}")
except:
print("无法获取签名")

# 尝试调用
B, N, H, D = 2, 5, 4, 64 # 较小的尺寸用于测试
tokens = torch.randn(B, N, H, D, device='cuda', dtype=torch.float32)
positions = torch.randint(0, 10, (B, N, 2), dtype=torch.int64, device='cuda')

try:
result = func(tokens, positions, 10000.0, 1.0)
print(f"调用成功! 输入: {tokens.shape}, 输出: {result.shape}")
except Exception as e:
print(f"调用失败: {e}")

analyze_functions()# 详细检查函数信息

33 changes: 33 additions & 0 deletions extern/CUT3R/src/croco/models/curope/my_static_build.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# partial_static_build.py
import torch
from torch.utils.cpp_extension import load
import os

# 清理缓存
os.system('rm -rf /root/.cache/torch_extensions/py310_cu124/curope')

print("部分静态链接(只链接 stdc++)...")
try:
curope = load(
name='curope',
sources=['curope.cpp', 'kernels.cu'],
extra_cflags=['-O3', '-fPIC'],
extra_cuda_cflags=[
'-O3',
'--ptxas-options=-v',
'--use_fast_math',
],
# 只静态链接 libstdc++,其他库动态链接
extra_ldflags=[
'-Wl,-Bstatic', '-lstdc++', '-Wl,-Bdynamic',
'-lpthread', # 确保 pthread 动态链接
],
verbose=True,
with_cuda=True
)
print("部分静态链接成功!")
print(f"编译后的模块位置: {curope.__file__}")
except Exception as e:
print(f"编译失败: {e}")
import traceback
traceback.print_exc()
Loading