11import os
22from setuptools import setup, Distribution
3+ import shutil
34
45packages = []
56package_data = {}
@@ -31,15 +32,31 @@ for lib in os.listdir(os.getenv("CUSTOM_DEVICE_ROOT")):
3132''')
3233 f.write('\n\n'.join(api_content))
3334
34- def write_version_py(filename='python/paddle_custom_device/intel_hpu/__init__.py'):
35+ def write_version_py(filename='python/paddle_custom_device/intel_hpu/__init__.py', source_path='python/paddle_custom_device/intel_hpu' ):
3536 dirname = os.path.dirname(filename)
3637 if not os.path.exists(dirname):
3738 os.makedirs(dirname)
39+
40+ # to get all the .py file under source_path (exclude __init__.py file)
41+ py_files = []
42+ if os.path.exists(source_path):
43+ for file in os.listdir(source_path):
44+ if file.endswith('.py') and file != '__init__.py':
45+ py_files.append(file[:-3]) # to remove the .py
46+
47+ # generate the import line
48+ import_statements = []
49+ for module in sorted(py_files): # to do sort
50+ import_statements.append(f"from .{module} import *")
51+
3852 cnt = '''# THIS FILE IS GENERATED FROM PADDLEPADDLE SETUP.PY
3953#
40- from .ops import *
41-
42- full_version = '@PADDLE_VERSION@'
54+ '''
55+ #to add all import lines
56+ cnt += '\n'.join(import_statements) + '\n\n'
57+
58+ #to add version info and function
59+ cnt += '''full_version = '@PADDLE_VERSION@'
4360synapse_version = @SYNAPSE_VERSION@
4461git_commit_id = '@GIT_HASH@'
4562custom_op_git_commit_id = '@CUSTOM_OP_GIT_HASH@'
@@ -74,11 +91,15 @@ def version():
7491 with open(filename, 'w') as f:
7592 f.write(cnt)
7693
94+ print(f"Generated {filename} with imports from {len(py_files)} modules: {py_files}")
95+
7796def write_init_py(filename='python/paddle_custom_device/__init__.py'):
7897 dirname = os.path.dirname(filename)
7998 if not os.path.exists(dirname):
8099 os.makedirs(dirname)
81- cnt = '''# THIS FILE IS GENERATED FROM PADDLEPADDLE SETUP.PY
100+ cnt = '''
101+ #
102+ # THIS FILE IS GENERATED FROM PADDLEPADDLE SETUP.PY
82103#
83104from . import intel_hpu # noqa: F401
84105
@@ -87,9 +108,56 @@ __all__ = [ # noqa
87108]
88109
89110'''
90- with open(filename, 'w ') as f:
111+ with open(filename, 'a+ ') as f:
91112 f.write(cnt)
92113
114+ def copy_paddlenlp_ops_files(source_path='../custom_ops/python/paddlenlp_ops', target_path='python/paddle_custom_device/intel_hpu'):
115+ if not os.path.exists(source_path):
116+ print(f"Source path {source_path} does not exist")
117+ return
118+
119+ try:
120+ os.makedirs(target_path, exist_ok=True)
121+ # to check whether the dir can be accessed
122+ if not os.access(source_path, os.R_OK):
123+ print(f"Cannot read source directory: {source_path}")
124+ return
125+
126+ for file in os.listdir(source_path):
127+ if file.endswith('.py') and file != '__init__.py':
128+ source_file = os.path.join(source_path, file)
129+
130+ #to check file can be read
131+ if not os.access(source_file, os.R_OK):
132+ print(f"Cannot read file: {source_file}")
133+ continue
134+
135+ target_file = os.path.join(target_path, file)
136+
137+ try:
138+ # to open and read source file
139+ with open(source_file, 'r') as f:
140+ content = f.read()
141+
142+ # to do modify
143+ modified_content = content.replace('import paddlenlp_ops', 'import paddle_custom_device.intel_hpu as paddlenlp_ops')
144+ modified_content = modified_content.replace('from paddlenlp_ops import', 'from paddle_custom_device.intel_hpu import')
145+
146+ # write to the target file
147+ with open(target_file, 'w') as f:
148+ f.write(modified_content)
149+
150+ print(f"Processed {file}")
151+
152+ except PermissionError:
153+ print(f"Permission denied when accessing {source_file}")
154+ except Exception as e:
155+ print(f"Error processing {file}: {e}")
156+
157+ except Exception as e:
158+ print(f"General error: {e}")
159+
160+ copy_paddlenlp_ops_files()
93161write_custom_op_api_py()
94162write_version_py()
95163write_init_py()
0 commit comments