@@ -482,77 +482,6 @@ def npu_runtime() -> ModuleType:
482482 return pytest .importorskip ("torch_npu" )
483483
484484
485- @pytest .fixture (scope = "module" )
486- def native_causal_conv1d (npu_runtime : ModuleType ) -> Callable [..., torch .Tensor ]:
487- """Load an operator-only library, not an application embedding Python modules."""
488- library_path = os .getenv ("XLLM_KDA_TEST_OP_LIBRARY" )
489- if library_path :
490- torch .ops .load_library (library_path )
491- if not hasattr (torch .ops .xllm_ops , "causal_conv1d" ):
492- pytest .fail ("Set XLLM_KDA_TEST_OP_LIBRARY to an operator-only library registering xllm_ops::causal_conv1d" )
493- return torch .ops .xllm_ops .causal_conv1d
494-
495-
496- @pytest .mark .skipif (not os .getenv ("XLLM_KDA_TEST_NPU_DEVICE" ), reason = "NPU device not configured" )
497- @pytest .mark .parametrize ("dtype" , [torch .float16 , torch .bfloat16 ])
498- @pytest .mark .parametrize ("kernel_width" , [2 , 4 ])
499- @pytest .mark .parametrize ("rows" , [1 , 4 ])
500- @pytest .mark .parametrize ("activation_mode" , [0 , 1 ])
501- @pytest .mark .parametrize ("run_mode" , [0 , 1 ])
502- @pytest .mark .parametrize ("packed" , [False , True ])
503- @torch .inference_mode ()
504- def test_native_causal_conv1d_output_state_and_graph_replay (
505- npu_runtime : ModuleType ,
506- native_causal_conv1d : Callable [..., torch .Tensor ],
507- dtype : torch .dtype ,
508- kernel_width : int ,
509- rows : int ,
510- activation_mode : int ,
511- run_mode : int ,
512- packed : bool ,
513- ) -> None :
514- device = torch .device (os .environ ["XLLM_KDA_TEST_NPU_DEVICE" ])
515- torch .npu .set_device (device )
516- generator = torch .Generator ().manual_seed (42 )
517- sequence_lengths = (rows , rows + 1 ) if packed else (rows , rows )
518- input_shape = (sum (sequence_lengths ), 16 ) if packed else (2 , rows , 16 )
519- query_start_loc = [0 , rows , sum (sequence_lengths )] if packed else []
520- inputs = torch .randn (input_shape , generator = generator ).to (device = device , dtype = dtype )
521- weight = torch .randn (kernel_width , 16 , generator = generator ).to (device = device , dtype = dtype )
522- state = torch .randn (2 , kernel_width - 1 , 16 , generator = generator ).to (device = device , dtype = dtype )
523- for _ in range (3 ):
524- native_causal_conv1d (inputs , weight , state , query_start_loc , activation_mode , run_mode )
525- torch .npu .synchronize ()
526- graph = npu_runtime .npu .NPUGraph ()
527- with npu_runtime .npu .graph (graph ):
528- captured = native_causal_conv1d (inputs , weight , state , query_start_loc , activation_mode , run_mode )
529- for _ in range (3 ):
530- input_cpu = torch .randn (input_shape , generator = generator ).to (dtype )
531- state_cpu = torch .randn (2 , kernel_width - 1 , 16 , generator = generator ).to (dtype )
532- expected_outputs = []
533- expected_states = []
534- for initial_state , sequence_input in zip (state_cpu , input_cpu .reshape (- 1 , 16 ).split (sequence_lengths )):
535- window = torch .cat ((initial_state , sequence_input ), dim = 0 )
536- convolved = functional .conv1d (
537- window .float ().t ().unsqueeze (0 ), weight .cpu ().float ().t ().unsqueeze (1 ), groups = 16
538- )
539- expected_outputs .append (convolved .squeeze (0 ).t ())
540- expected_states .append (window [- (kernel_width - 1 ) :])
541- expected = torch .cat (expected_outputs ).reshape (input_shape )
542- if activation_mode == 1 :
543- expected = functional .silu (expected )
544- inputs .copy_ (input_cpu )
545- state .copy_ (state_cpu )
546- graph .replay ()
547- torch .npu .synchronize ()
548- eager_state = state_cpu .to (device )
549- eager = native_causal_conv1d (inputs , weight , eager_state , query_start_loc , activation_mode , run_mode )
550- torch .testing .assert_close (captured , eager , rtol = 0 , atol = 0 )
551- torch .testing .assert_close (state , eager_state , rtol = 0 , atol = 0 )
552- torch .testing .assert_close (state .cpu (), torch .stack (expected_states ), rtol = 0 , atol = 0 )
553- torch .testing .assert_close (captured .cpu (), expected .to (dtype ), rtol = 2 * torch .finfo (dtype ).eps , atol = 1e-3 )
554-
555-
556485@pytest .mark .skipif (not os .getenv ("XLLM_KDA_TEST_NPU_DEVICE" ), reason = "NPU device not configured" )
557486@pytest .mark .parametrize ("num_tokens" , [1 , 4 , 17 , 128 ])
558487@torch .inference_mode ()
0 commit comments