acalejos / exgboost

Elixir bindings to the XGBoost C API (https://xgboost.readthedocs.io/en/stable/c.html) using Native Implemented Functions (NIFs)
Apache License 2.0
160 stars 7 forks source link

Incompatibility with EXLA backend #41

Closed camfeghali closed 3 months ago

camfeghali commented 5 months ago

Hey, I setup a livebook where at some point I set EXLA as the global backend:

Nx.global_default_backend(EXLA.Backend)

I do this because before trying to run EXGBoost, I run KNN from Scholar.

When I try to train my EXGBoost model though, It complains about not being able to encode Nx.Tensors by throwing the following error:

** (FunctionClauseError) no function clause matching in Jason.Encoder.Nx.Tensor.encode/2    

    The following arguments were given to Jason.Encoder.Nx.Tensor.encode/2:

        # 1
        #Nx.Tensor<
          f32
          EXLA.Backend<host:0, 0.2691417836.2728001575.112360>
          NaN
        >

        # 2
        {#Function<3.65781566/3 in Jason.Encode.escape_json>,
         #Function<1.65781566/3 in Jason.Encode.map_naive>}

    Attempted function clauses (showing 4 out of 4):

        def encode(%Nx.Tensor{data: %Nx.BinaryBackend{state: <<32704::integer-native-size(16)>>}}, _opts)
        def encode(%Nx.Tensor{data: %Nx.BinaryBackend{state: <<32256::integer-native-size(16)>>}}, _opts)
        def encode(%Nx.Tensor{data: %Nx.BinaryBackend{state: <<2_143_289_344::integer-native-size(32)>>}}, _opts)
        def encode(%Nx.Tensor{data: %Nx.BinaryBackend{state: <<9_221_120_237_041_090_560::integer-native-size(64)>>}}, _opts)

    (exgboost 0.5.0) lib/exgboost/internal.ex:114: Jason.Encoder.Nx.Tensor.encode/2
    (jason 1.4.1) lib/encode.ex:188: Jason.Encode.map_naive/3
    (jason 1.4.1) lib/encode.ex:37: Jason.Encode.encode/2
    (jason 1.4.1) lib/jason.ex:162: Jason.encode!/2
    (exgboost 0.5.0) lib/exgboost/dmatrix.ex:310: EXGBoost.DMatrix.from_tensor/2
    (exgboost 0.5.0) lib/exgboost.ex:249: EXGBoost.train/3
    #cell:zj4df6g37mdj3th6:4: (file)

Is this expected behaviour ?

Maybe I can only specify the EXLA backend as a parameter when running KNN ?

KNN.fit(train_data, train_labels, num_neighbors: best_neighbors, num_classes: 3)

Any help would be much appreciated!

acalejos commented 5 months ago

This is expected. It's currently listed at the bottom of the README as a known limitation.

You can read more here: https://github.com/acalejos/exgboost/issues/22

You'll have to backend transfer to the binary backend first.

This is the next issue up to be resolved

camfeghali commented 5 months ago

Okay! Thanks for getting back to me :)

acalejos commented 3 months ago

@camfeghali Just pushed a patch for 0.5.1 that should fix this. Let me know if it doesn't!