Skip to content

Commit

Permalink
Hipify config (#14)
Browse files Browse the repository at this point in the history
* add json support for hipify_cli.py

* add json

* update header_includes in json

* update cmake to call config json and bug in hip_cli

* raise error if proj_dir or json config not mentioned in cli

* if proj_dir is not mentioned working directory is proj dir

* typo

* cwd gives build dir , hack to get proj_dir

* Update README.md

* Update README.md

* sample file is part of readme

* Update README.md

* add default hipify_dict_dump

* remove dump_dict_file from json

* dump_dict_file called before getting assigned

* output_directory is also relative path to hipify_config.json

* raise valueerror when dump_dict_file not defined

* Update hipify_cli.py

* Update README.md

* Update README.md
  • Loading branch information
bmedishe committed Mar 16, 2022
1 parent 0f7fc0d commit 20826e4
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 16 deletions.
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,21 @@ Note: Update the CMAKE_MODULE_PATH list accordingly, if the hipify_torch repo is
Above lines trigger the hipify script for all sources & header files under the `CUDA_SOURCE_DIR`

## Through python

### hipify_cli.py
hipify_cli.py takes arguments through command line or a json file(hipify_config.json)
```
python hipify_cli.py --config-json hipify_config.json
```
hipify_config.json
```json
{
"project_directory": <relative path of project_directory to config_json>,
"output_directory" : <relative path of output_directory to config_json>,
"header_include_dirs": [<rel path to proj_dir of directories where headers are present>],
"includes":[<rel path to proj_dir of directories to be hipified> ],
}
```
### hipify.hipify_python
```
from <path to hipify_torch>.hipify.hipify_python import hipify
```
Expand Down
2 changes: 1 addition & 1 deletion cmake/Hipify.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ function(hipify)
if(HIPIFY_CONFIG_FILE)
set(HIPIFY_COMMAND
${HIPIFY_DIR}/hipify_cli.py
--config-file ${HIPIFY_CONFIG_FILE}
--config-json ${HIPIFY_CONFIG_FILE}
--dump-dict-file ${HIPIFY_DICT_FILE}
)
elseif(HIPIFY_CUDA_SOURCE_DIR)
Expand Down
68 changes: 54 additions & 14 deletions hipify_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ def main():
parser.add_argument(
'--project-directory',
type=str,
help="The root of the project. (default: %(default)s)",
required=True)
help="The root of the project. (default: %(default)s)")

parser.add_argument(
'--output-directory',
Expand Down Expand Up @@ -57,24 +56,65 @@ def main():
help="The file to Store the return dict output after hipification",
required=False)

args = parser.parse_args()
print(args)
parser.add_argument(
'--config-json',
type=str,
help="relative path of hipify config json which contains arguments to hipify",
required=False)


out_dir = args.project_directory
if args.output_directory:
out_dir = args.output_directory
args = parser.parse_args()
if(os.path.exists(args.config_json)):
with open(args.config_json) as jsonf:
json_args = json.load(jsonf)
if(json_args.get('project_directory') is not None):
project_directory = os.path.join(os.path.dirname(args.config_json), json_args['project_directory'])
else:
raise ValueError('relative path to project_dir to config_json should be mentioned')
if(json_args.get('output_directory') is not None):
output_directory = os.path.join(os.path.dirname(args.config_json), json_args['output_directory'])
else:
output_directory = project_directory
if(json_args.get('includes') is not None):
includes = json_args['includes']
else:
includes = ['*']
if(json_args.get('header_include_dirs') is not None):
header_include_dirs = json_args['header_include_dirs']
else:
header_include_dirs = []
if(json_args.get('ignores') is not None):
ignores = json_args['ignores']
else:
ignores = []
else:
if args.project_directory is not None:
project_directory=args.project_directory;
else:
raise ValueError('If not using config json , project_directory should be mentioned in commadline')
if args.output_directory:
output_directory = args.output_directory
else:
output_directory = args.project_directory
includes=args.includes
ignores=args.ignores
header_include_dirs=args.header_include_dirs
dump_dict_file = args.dump_dict_file
print("project_directory :",project_directory , " output_directory: ", output_directory, " includes: ", includes, " ignores: ", ignores, " header_include_dirs: ", header_include_dirs)

HipifyFinalResult = hipify_python.hipify(
project_directory=args.project_directory,
output_directory=out_dir,
includes=args.includes,
ignores=args.ignores,
header_include_dirs=args.header_include_dirs,
project_directory=project_directory,
output_directory=output_directory,
includes=includes,
ignores=ignores,
header_include_dirs=header_include_dirs,
is_pytorch_extension=True)

if args.dump_dict_file:
with open(args.dump_dict_file, 'w') as dict_file:
if dump_dict_file:
with open(dump_dict_file, 'w') as dict_file:
dict_file.write(json.dumps(HipifyFinalResult))
else:
raise ValueError ('dump_dict_file should be defined')

if __name__ == "__main__":
main()

0 comments on commit 20826e4

Please sign in to comment.