# TypeError: Expected UnionAll, got Type{Machine{...}}

**URL:** <https://discourse.julialang.org/t/typeerror-expected-unionall-got-type-machine/116354>\
**Category:** Machine Learning\
**Tags:** mlj\
**Created:** [June 28, 2024, 12:05pm UTC](https://discourse.julialang.org/t/typeerror-expected-unionall-got-type-machine/116354 "2024-06-28T12:05:00Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![niltsz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/niltsz/32/30060_2.png) [@niltsz](https://discourse.julialang.org/u/niltsz)\
**Post date:** [June 28, 2024, 12:05pm UTC](https://discourse.julialang.org/t/typeerror-expected-unionall-got-type-machine/116354/1 "2024-06-28T12:05:00Z")

</div>

I want to save a trained MLJ machine (EvoTreeRegressor) on the cluster and then open it locally on my computer. On the cluster, I save the machine with

```julia
MLJ.save("path/on/cluster/machine.jls", mach)

```

and then try to load it with

```julia
mach = machine("path/on/local/computer/machine.jls")

```

However, I get the following error message:

```julia
TypeError: in Type{...} expression, expected UnionAll, got Type{Machine{EvoTreeRegressor{EvoTrees.MSE}, EvoTreeRegressor{EvoTrees.MSE}}}

```

and the stacktrace

```julia
[1] deserialize_datatype(s::Serialization.Serializer{IOStream}, full::Bool)
    @ Serialization /cluster/easybuild/broadwell/software/Julia/1.10.0-linux-x86_64/share/julia/stdlib/v1.10/Serialization/src/Serialization.jl:1423
  [2] handle_deserialize(s::Serialization.Serializer{IOStream}, b::Int32)
    @ Serialization /cluster/easybuild/broadwell/software/Julia/1.10.0-linux-x86_64/share/julia/stdlib/v1.10/Serialization/src/Serialization.jl:867
  [3] deserialize(s::Serialization.Serializer{IOStream})
    @ Serialization /cluster/easybuild/broadwell/software/Julia/1.10.0-linux-x86_64/share/julia/stdlib/v1.10/Serialization/src/Serialization.jl:814
  [4] handle_deserialize(s::Serialization.Serializer{IOStream}, b::Int32)
    @ Serialization /cluster/easybuild/broadwell/software/Julia/1.10.0-linux-x86_64/share/julia/stdlib/v1.10/Serialization/src/Serialization.jl:882
  [5] deserialize(s::Serialization.Serializer{IOStream})
    @ Serialization /cluster/easybuild/broadwell/software/Julia/1.10.0-linux-x86_64/share/julia/stdlib/v1.10/Serialization/src/Serialization.jl:814
  [6] handle_deserialize(s::Serialization.Serializer{IOStream}, b::Int32)
    @ Serialization /cluster/easybuild/broadwell/software/Julia/1.10.0-linux-x86_64/share/julia/stdlib/v1.10/Serialization/src/Serialization.jl:920
  [7] deserialize
    @ Serialization /cluster/easybuild/broadwell/software/Julia/1.10.0-linux-x86_64/share/julia/stdlib/v1.10/Serialization/src/Serialization.jl:814 [inlined]
  [8] deserialize(s::IOStream)
    @ Serialization /cluster/easybuild/broadwell/software/Julia/1.10.0-linux-x86_64/share/julia/stdlib/v1.10/Serialization/src/Serialization.jl:801
  [9] open(f::typeof(Serialization.deserialize), args::String; kwargs::@Kwargs{})
    @ Base ./io.jl:396
 [10] open
    @ ./io.jl:393 [inlined]
 [11] deserialize
    @ /cluster/easybuild/broadwell/software/Julia/1.10.0-linux-x86_64/share/julia/stdlib/v1.10/Serialization/src/Serialization.jl:811 [inlined]
 [12] machine(file::String)
    @ MLJBase ~/.julia/packages/MLJBase/iIhiI/src/machines.jl:398
 [13] top-level scope
    @ REPL[13]:1

```

On the cluster where the machine is saved, I am using the following versions:  
MLJ v0.20.3  
MLJTuning v0.8.4  
EvoTrees v0.16.7

(note that the Model I am saving is _not_ a tuned model, but I import it anyways for a training a different model in the same skript)

On the local computer where I want to load the machine I am using

MLJ v0.20.3  
EvoTrees v0.16.7.

Do you have any idea what is going on here?

---

<div class="post-metadata">

**Author:** ![ablaom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ablaom/32/4889_2.png) [@ablaom](https://discourse.julialang.org/u/ablaom)\
**Post date:** [June 30, 2024, 9:01pm UTC](https://discourse.julialang.org/t/typeerror-expected-unionall-got-type-machine/116354/2 "2024-06-30T21:01:15Z")

</div>

Is it possible to confirm that the issue persists when you ensure the two machines are using _identical_ package environments (Manifest.toml files) and of course identical Julia versions?

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [July 1, 2024, 2:53am UTC](https://discourse.julialang.org/t/typeerror-expected-unionall-got-type-machine/116354/3 "2024-07-01T02:53:46Z")

</div>

Give `versioninfo()` of both machines.

Have you tried loading the machine on the cluster itself, without involving your local machine?

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [July 1, 2024, 3:10am UTC](https://discourse.julialang.org/t/typeerror-expected-unionall-got-type-machine/116354/4 "2024-07-01T03:10:07Z")

</div>

> [@niltsz](#):
>
> However, I get the following error message:
> 
> ```julia
> TypeError: in Type{...} expression, expected UnionAll, got Type{Machine{EvoTreeRegressor{EvoTrees.MSE}, EvoTreeRegressor{EvoTrees.MSE}}}
> 
> ```

It seems from the error message that the issue is a mismatch of MLJBase.jl versions. Please give the version of MLJBase in the manifest on both machines.

In particular, this commit introduces a third type parameter to `Machine`:

> <https://github.com/JuliaAI/MLJBase.jl/commit/7ae58213dd4e158bf885df81498b4db2af3a19f5>
>
> oops

So it seems the version of MLJBase on the cluster is newer than on your local computer.

---

<div class="post-metadata">

**Author:** ![ablaom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ablaom/32/4889_2.png) [@ablaom](https://discourse.julialang.org/u/ablaom)\
**Post date:** [July 1, 2024, 3:46am UTC](https://discourse.julialang.org/t/typeerror-expected-unionall-got-type-machine/116354/5 "2024-07-01T03:46:07Z")

</div>

> [@nsajko](#):
>
> In particular, this commit introduces a third type parameter to `Machine`:

Ahhh. Good observation @nsajko . That’s seems like the likely issue.

---

<div class="post-metadata">

**Author:** ![niltsz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/niltsz/32/30060_2.png) [@niltsz](https://discourse.julialang.org/u/niltsz)\
**Post date:** [July 1, 2024, 11:21am UTC](https://discourse.julialang.org/t/typeerror-expected-unionall-got-type-machine/116354/6 "2024-07-01T11:21:37Z")

</div>

Very good observation, I used MLJBase 1.2.1 and MLJBase 1.3.0. Making sure the MLJBase version is the same indeed solves the issue. Thank you very much

---

<div class="post-metadata">

**Author:** ![niltsz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/niltsz/32/30060_2.png) [@niltsz](https://discourse.julialang.org/u/niltsz)\
**Post date:** [July 1, 2024, 11:31am UTC](https://discourse.julialang.org/t/typeerror-expected-unionall-got-type-machine/116354/7 "2024-07-01T11:31:28Z")

</div>

In case someone else (or future me) stumbles into this problem again: If you save the machine with MLJBase 1.3.0 and load it with MLJBase 1.2.1, you get the TypeError mentioned above.  
However, if you save the machine with MLJBase 1.2.1 and then try to load it with MLJBase 1.3.0, you get the error message

```julia
ERROR: MethodError: no method matching deserialize(::Serialization.Serializer{IOStream}, ::Type{Machine{EvoTreeRegressor{EvoTrees.MSE}, true}})

Closest candidates are:
  deserialize(::Serialization.AbstractSerializer, ::Type{Core.MethodInstance})
   @ Serialization ~/software/julia-1.10.2/share/julia/stdlib/v1.10/Serialization/src/Serialization.jl:1090
  deserialize(::Serialization.AbstractSerializer, ::Type{Core.PhiNode})
   @ Serialization ~/software/julia-1.10.2/share/julia/stdlib/v1.10/Serialization/src/Serialization.jl:1121
  deserialize(::Serialization.AbstractSerializer, ::Type{Core.TypeName})
   @ Serialization ~/software/julia-1.10.2/share/julia/stdlib/v1.10/Serialization/src/Serialization.jl:1297

```

with the stacktrace

```julia
[1] handle_deserialize(s::Serialization.Serializer{IOStream}, b::Int32)
    @ Serialization ~/software/julia-1.10.2/share/julia/stdlib/v1.10/Serialization/src/Serialization.jl:883
  [2] deserialize(s::Serialization.Serializer{IOStream})
    @ Serialization ~/software/julia-1.10.2/share/julia/stdlib/v1.10/Serialization/src/Serialization.jl:814
  [3] handle_deserialize(s::Serialization.Serializer{IOStream}, b::Int32)
    @ Serialization ~/software/julia-1.10.2/share/julia/stdlib/v1.10/Serialization/src/Serialization.jl:920
  [4] deserialize
    @ ~/software/julia-1.10.2/share/julia/stdlib/v1.10/Serialization/src/Serialization.jl:814 [inlined]
  [5] deserialize(s::IOStream)
    @ Serialization ~/software/julia-1.10.2/share/julia/stdlib/v1.10/Serialization/src/Serialization.jl:801
  [6] open(f::typeof(Serialization.deserialize), args::String; kwargs::@Kwargs{})
    @ Base ./io.jl:396
  [7] open
    @ ./io.jl:393 [inlined]
  [8] deserialize
    @ ~/software/julia-1.10.2/share/julia/stdlib/v1.10/Serialization/src/Serialization.jl:811 [inlined]
  [9] machine(file::String)
    @ MLJBase ~/.julia/packages/MLJBase/hoZmq/src/machines.jl:404
 [10] top-level scope
    @ REPL[6]:1

```

As mentioned by @nsajko make sure that the MLJBase version is the same on both computers.
