Repository navigation
feat(ftorch): finish CTSM FTorch integration: add system test calling FTorch inference - #4265
Open
johnpaulalex wants to merge 7 commits into
Open
johnpaulalex wants to merge 7 commits into
johnpaulalex wants to merge 7 commits into
Conversation
added 4 commits
October 18, 2026 20:09
Registers ESCOMP/FTorch_interface as TopLevelRequired CTSM submodule. It is 'TopLevel' because CESM already has it. It is 'Required' because it needs to show up as stubs by default (enabling linking in F90 code that calls ftorch, behind gated bool vars).
…l for generating toy test models As a tool, this creates toy models for testing. As a library, it converts a PyTorch nn.Module to a TorchScript file.
Wires an end-to-end runtime verification test for FTorch inference into CTSM initialization and CIME system testing: 1. Adds generic ftorch testmod to turn on USE_FTORCH, and on top of that, adds ftorch_demo testmod to enable new use_ftorch_demo logical namelist variable, and copies in constant_model.pt. 2. Imports FTorch_cesm_interface in clm_initializeMod.F90 (initialize2) and, when use_ftorch_demo = .true., runs constant_model.pt inference, logs the output tensor, and deletes the model. 3. Registers two Derecho aux_clm system tests (intel and gnu) in testlist_clm.xml: - SMS_D_Ld1.f10_f10_mg37.I2000Clm60Sp: Tests multi-process MPI on a global 10x15 grid, verifying dynamic linking of multi-threaded libtorch_cpu.so under Cray MPI across distributed ranks. - SMS_D_Ld1_Mmpi-serial.1x1_brazil.I2000Clm60SpRsGs: Tests single-point mpi-serial execution on 1x1_brazil, representing the lightweight single-point workflow heavily relied upon by the LEAP and M2LInES machine learning research communities without requiring full MPI libraries.
❌ Docs build failedBuild logs |
Points the libraries/FTorch gitlink to v0.0.6 (6d03d7c) matching fxtag in .gitmodules, resolving the git-fleximod check-cleanliness CI failure in GitHub Actions.
Contributor
Author
|
@ekluzek Here's the other half of CTSM FTorch support (paired with ESCOMP/FTorch_interface#2). |
❌ Docs build failedBuild logs |
Contributor
|
Thanks, @johnpaulalex. It looks like some of the documentation build tests failed. Are you able to correct these? Who's been handling the ftorch feature request who would be able to review this PR & do testing? Marking as next to discuss at the meeting Thursday. |
Contributor
Author
|
hey @wwieder , I pinged a couple people on the Ftorch_interface PR, it's only the second PR ever so I guess it's early days. Docs build is all fixed. |
Contributor
|
This isn't critical for the CLM6 release, so we decided to mark this with a later milestone, but reviewing the code isn't a priority right now. |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description of changes
Add FTorch hooks at various places to enable FTorch inference and all necessary backing infra.
Relies on FTorch_Interface PR ESCOMP/FTorch_interface#2
Overview (Execution sequencing)
Listed in execution order, with the deltas from this PR's commits called out:
0. Human user prep:
Running-with-FTorch.rstdocumentation in the User's Guide for build and execution instructions../tools/ftorch/export_torchscript) to serialize trained PyTorch models to TorchScript (.pt) format.1a. Source Code Initialization (Pre-build)
.gitmodules, defines thelibraries/FTorchsubmodule pointing atESCOMP/FTorch_interface(fxrequired = ToplevelRequired).1b. Setup & Configuration (
create_test/case.setup)USE_FTORCH=FALSEfrom the CIME environment XMLs (cime/CIME/build.py).testlist_clm.xmlin Commit 4): Two system tests (SMS_D_Ld1.f10_f10_mg37.I2000Clm60SpandSMS_D_Ld1_Mmpi-serial.1x1_brazil.I2000Clm60SpRsGswithtestmods="clm/default--clm/nofireemis--clm/ftorch_demo") added incime_config/testdefs/testlist_clm.xmlfor bothintelandgnuon Derecho. They compile withUSE_FTORCH=TRUEand execute runtime FTorch inference during CTSM initialization: one tests multi-process MPI on a global 10x15 grid (SMS_D_Ld1.f10_f10_mg37.I2000Clm60Sp), and the second tests single-pointmpi-serialexecution on1x1_brazil(SMS_D_Ld1_Mmpi-serial.1x1_brazil.I2000Clm60SpRsGs, representing the LEAP/M2LInES single-point workflow).clm/ftorch_demo, whoseinclude_user_modsautomatically pulls in../ftorch(clm/ftorch/shell_commands, which sets./xmlchange USE_FTORCH=TRUE), whileclm/ftorch_demo/shell_commandsstages the pre-generated TorchScript model (constant_model.pt) into$RUNDIRandclm/ftorch_demo/user_nl_clmenablesuse_ftorch_demo = .true..2. Compilation (
case.build)cime/CIME/build.pyinvokes$SRCROOT/libraries/FTorch/buildlib, andcime/CIME/Tools/Makefilepasses-DUSE_FTORCHand links-lftorch/-ltorch_cpuwhenUSE_FTORCH=TRUE(or links only thelibftorch_wrapperstub whenUSE_FTORCH=FALSE).ESCOMP/FTorch_interfacePR):USE_FTORCH=FALSE(default):libraries/FTorch/buildlibskips checkingTORCH_DIRand compiles onlyFTorch_cesm_interface.F90intolibftorch_wrapperusing the#elsestub (which callsshr_abort_abortif invoked at runtime).USE_FTORCH=TRUE:libraries/FTorch/buildlibvalidates that the C++ inferenceTORCH_DIRexists, compiles the FTorch Fortran-to-C++ bridge andFTorch_cesm_interface.F90(libftorch_wrapper), and placesftorch.modin${CMAKE_BINARY_DIR}/modulesto matchINCLDIR += -I$(FTORCH_DIR)/modulesincime/CIME/Tools/Makefile.ftorchandftorch_wrapperto build cleanly as shared libraries on Cray for bothUSE_FTORCH=TRUEandUSE_FTORCH=FALSE.buildlibrunsFTMakefileunconditionally soUSE_FTORCHchanges are re-evaluated by CMake.3. Runtime Execution (
mpiexec cesm.exe)libtorch_cpu.soandlibftorchvia therpath($(TORCH_DIR)/liband$(FTORCH_DIR)/src) embedded intocesm.exeat link time.if (use_ftorch_demo)on all MPI ranks (with zero#ifdef USE_FTORCHinclm_initializeMod.F90, matching the CESM/CAM convention, and gating onlywrite(iulog,*)log messages onmasterproc),clm_initializeMod.F90callstorch_model_loadand maps Fortran arrays to PyTorch tensors viatorch_tensor_from_array.torch_model_forwardand deallocates memory viatorch_delete.Specific notes
Contributors other than yourself, if any:
CTSM issues resolved or otherwise addressed, if any:
If answers are expected to change, describe (delete this line otherwise):
clm_initializeblock and logs status output whenuse_ftorch_demois.true.. It does not mutate any prognostic physics state.Any user interface changes (namelist or namelist defaults changes)?
use_ftorch_demo(default:.false.) innamelist_definition_ctsm.xml.Testing planned or performed, if any:
python/ctsm/test/test_unit_export_torchscript.pypass cleanly both without PyTorch (3/3on local Mac) and withpytorch-cpuinctsm_pylib(3/3on Derecho).python3 cime/scripts/fortran_unit_testing/run_tests.py --test-spec-dir src) builds and passes cleanly (59/59PASS).(USE_FTORCH, use_ftorch_demo)system test permutations on NCAR Derecho (-t ftorch_perm_1):USE_FTORCH=TRUE,use_ftorch_demo=.true., Intel MPI):SMS_D_Ld1.f10_f10_mg37.I2000Clm60Sp.derecho_intel.clm-default--clm-nofireemis--clm-ftorch_demo(PASS;FTORCH INFERENCE SUCCESSFUL: output = 1.00000000000000inlnd.log).USE_FTORCH=TRUE,use_ftorch_demo=.true., GNUmpi-serial):SMS_D_Ld1_Mmpi-serial.1x1_brazil.I2000Clm60SpRsGs.derecho_gnu.clm-default--clm-nofireemis--clm-ftorch_demo(PASS;FTORCH INFERENCE SUCCESSFUL: output = 1.00000000000000inlnd.log).USE_FTORCH=TRUE,use_ftorch_demo=.false., Intel MPI):SMS_D_Ld1.f10_f10_mg37.I2000Clm60Sp.derecho_intel.clm-default--clm-nofireemis--clm-ftorch(PASS; clean no-op with zeroFTORCHlines inlnd.log).USE_FTORCH=FALSE,use_ftorch_demo=.false., GNUmpi-serial):SMS_D_Ld1_Mmpi-serial.1x1_brazil.I2000Clm60SpRsGs.derecho_gnu.clm-default--clm-nofireemis(PASS; compiles and links without#ifdef USE_FTORCHagainstFTorch_cesm_interfacestub).USE_FTORCH=FALSE,use_ftorch_demo=.true., GNUmpi-serial):SMS_D_Ld1_Mmpi-serial.1x1_brazil.I2000Clm60SpRsGs.derecho_gnu.clm-default--clm-nofireemis--clm-ftorch_demo_only(RUNaborts as expected withERROR: Using FTorch Interface without USE_FTORCH=TRUE).USE_FTORCH=FALSE, default):SMS_D_Ld5.f10_f10_mg37.IHistClm60BgcCrop.derecho_intel.interim_restartbuilds and runs uninterrupted without FTorch (PASS).fab11fa) and PR 2 (212bda8d0) on NCAR Derecho (TaskT15, output root/glade/derecho/scratch/jpalex/tests_ftorch_t15):SMS_D_Ld1.f10_f10_mg37.I2000Clm60Sp.derecho_intel.clm-default--clm-nofireemis--clm-ftorch_demo(PASS; confirmedFTORCH INFERENCE SUCCESSFUL: output = 1.00000000000000)./glade/derecho/scratch/jpalex/tests_ftorch_rerun): all standalone CTSM tests (SMS_D_Ld1...clm-ftorch_demoand genericSMS_D_Ld1...clm-ftorch) and coupled CESM checkout tests (SMS_D_Ld1...clm-ftorch_demoIntel MPI, Intel MPIUSE_FTORCH=FALSE, and GNUmpi-serialUSE_FTORCH=FALSE) passed.Requirements before merge:
aux_clmsystem tests and full(USE_FTORCH, use_ftorch_demo)permutation matrix.Running-with-FTorch.rstto the User's Guide underrunning-special-cases.Google Gemini Argon 4 was used to write the code and tests, followed by human-guided verification.