-
Notifications
You must be signed in to change notification settings - Fork 6
gpu support #256
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
gpu support #256
Changes from 5 commits
33f0039
671d905
72cce1a
8b44d5a
895935b
aa5cb78
76a5748
305efeb
b008781
b74426e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,22 +1,29 @@ | ||
| export prepare_data | ||
|
|
||
| function prepare_data(hm, data::KeyedArray; kwargs...) | ||
| predictors_forcing, targets = get_prediction_target_names(hm) | ||
| function prepare_data(hm, data::KeyedArray; cfg=DataConfig(), kwargs...) | ||
| predictors, forcings, targets = get_prediction_target_names(hm) | ||
| # KeyedArray: use () syntax for views that are differentiable | ||
| return (data(predictors_forcing), data(targets)) | ||
| X_arr = Array(data(predictors)) | ||
| forcings_nt = NamedTuple([forcing => Array(data(forcing)) for forcing in forcings]) | ||
| targets_nt = NamedTuple([target => Array(data(target)) for target in targets]) | ||
| return ((X_arr, forcings_nt), targets_nt) | ||
| end | ||
|
|
||
| function prepare_data(hm, data::AbstractDimArray; kwargs...) | ||
Qfl3x marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| predictors_forcing, targets = get_prediction_target_names(hm) | ||
| predictors, forcings, targets = get_prediction_target_names(hm) | ||
| # KeyedArray: use () syntax for views that are differentiable | ||
| X_arr = data[variable = At(predictors)] | ||
| forcings_nt = NamedTuple([forcing => data[variable = At(forcing)] for forcing in forcings]) | ||
| targets_nt = NamedTuple([target => data[variable = At(target)] for target in targets]) | ||
| # DimArray: use [] syntax (copies, but differentiable) | ||
| return (data[variable = At(predictors_forcing)], data[variable = At(targets)]) | ||
| return ((X_arr, forcings_nt), targets_nt) | ||
| end | ||
|
|
||
| function prepare_data(hm, data::DataFrame; array_type = :KeyedArray, drop_missing_rows = true) | ||
| predictors_forcing, targets = get_prediction_target_names(hm) | ||
| predictors, forcings, targets = get_prediction_target_names(hm) | ||
|
|
||
| all_predictor_cols = unique(vcat(values(predictors_forcing)...)) | ||
| col_to_select = unique([all_predictor_cols; targets]) | ||
| # all_predictor_cols = unique(vcat(values(predictors_forcing)...)) | ||
| col_to_select = unique([predictors; forcings; targets]) | ||
|
|
||
| # subset to only the cols we care about | ||
| sdf = data[!, col_to_select] | ||
|
|
@@ -83,34 +90,39 @@ Returns a tuple of (predictors_forcing, targets) names. | |
| """ | ||
| function get_prediction_target_names(hm) | ||
| targets = hm.targets | ||
| predictors_forcing = Symbol[] | ||
| predictors = Symbol[] | ||
| forcings = Symbol[] | ||
| for prop in propertynames(hm) | ||
| if occursin("predictors", string(prop)) | ||
| val = getproperty(hm, prop) | ||
| if isa(val, AbstractVector) | ||
| append!(predictors_forcing, val) | ||
| append!(predictors, val) | ||
| elseif isa(val, Union{NamedTuple, Tuple}) | ||
| append!(predictors_forcing, unique(vcat(values(val)...))) | ||
| append!(predictors, unique(vcat(values(val)...))) | ||
| end | ||
| end | ||
| end | ||
| for prop in propertynames(hm) | ||
| if occursin("forcing", string(prop)) | ||
| val = getproperty(hm, prop) | ||
| if isa(val, AbstractVector) | ||
| append!(predictors_forcing, val) | ||
| append!(forcings, val) | ||
| elseif isa(val, Union{Tuple, NamedTuple}) | ||
| append!(predictors_forcing, unique(vcat(values(val)...))) | ||
| append!(forcings, unique(vcat(values(val)...))) | ||
| end | ||
| end | ||
| end | ||
| predictors_forcing = unique(predictors_forcing) | ||
| # predicto | ||
| # predictors_forcing = unique(predictors_forcing) | ||
Qfl3x marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| if isempty(predictors_forcing) | ||
| @warn "Note that you don't have predictors or forcing variables." | ||
| if isempty(predictors) | ||
| @warn "Note that you don't have predictors variables." | ||
| end | ||
| if isempty(forcings) | ||
| @warn "Note that you don't have forcing variables." | ||
| end | ||
| if isempty(targets) | ||
| @warn "Note that you don't have target names." | ||
| end | ||
| return predictors_forcing, targets | ||
| return predictors, forcings, targets | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The function now returns |
||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -53,7 +53,7 @@ function save_observations!(file_name, target_names, yobs, train_or_val_name) | |
| end | ||
|
|
||
| function to_named_tuple(ka, target_names) | ||
| arrays = [Array(ka(variable = k)) for k in target_names] | ||
| arrays = [Array(ka[k]) for k in target_names] | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The change from |
||
| return NamedTuple{Tuple(target_names)}(arrays) | ||
| end | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,14 +18,14 @@ Main loss function for hybrid models that handles both training and evaluation m | |
| - `(loss_values, st, ŷ)`: NamedTuple of losses, state and predictions | ||
| """ | ||
| function compute_loss( | ||
| HM::LuxCore.AbstractLuxContainerLayer, ps, st, (x, (y_t, y_nan)); | ||
| HM::LuxCore.AbstractLuxContainerLayer, ps, st, ((x, forcings), (y_t, y_nan)); | ||
| logging::LoggingLoss | ||
| ) | ||
|
|
||
| targets = HM.targets | ||
| ext_loss = extra_loss(logging) | ||
| if logging.train_mode | ||
| ŷ, st = HM(x, ps, st) | ||
| ŷ, st = HM((x, forcings), ps, st) | ||
| loss_value = _compute_loss(ŷ, y_t, y_nan, targets, training_loss(logging), logging.agg) | ||
| # Add extra_loss if provided | ||
| if ext_loss !== nothing | ||
|
|
@@ -34,7 +34,7 @@ function compute_loss( | |
| end | ||
| stats = NamedTuple() | ||
| else | ||
| ŷ, _ = HM(x, ps, LuxCore.testmode(st)) | ||
| ŷ, _ = HM((x, forcings), ps, LuxCore.testmode(st)) | ||
| loss_value = _compute_loss(ŷ, y_t, y_nan, targets, loss_types(logging), logging.agg) | ||
| # Add extra_loss entries if provided | ||
| if ext_loss !== nothing | ||
|
|
@@ -105,9 +105,11 @@ _get_target_ŷ(ŷ, y_t, target) = | |
| function assemble_loss(ŷ, y, y_nan, targets, loss_spec) | ||
| return [ | ||
| begin | ||
| y_t = _get_target_y(y, target) | ||
| ŷ_t = _get_target_ŷ(ŷ, y_t, target) | ||
| _apply_loss(ŷ_t, y_t, _get_target_nan(y_nan, target), loss_spec) | ||
| y_t = y[target]# _get_target_y(y, target) | ||
| ŷ_t = ŷ[target]#_get_target_ŷ(ŷ, y_t, target) | ||
|
||
| y_nan_t = y_nan[target] | ||
| _apply_loss(ŷ_t, y_t, y_nan_t, loss_spec) | ||
| # _apply_loss(ŷ_t, y_t, _get_target_nan(y_nan, target), loss_spec) | ||
|
Comment on lines
+110
to
+112
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The use of y_nan_t = _get_target_nan(y_nan, target)
_apply_loss(ŷ_t, y_t, y_nan_t, loss_spec) |
||
| end | ||
| for target in targets | ||
| ] | ||
|
|
@@ -163,6 +165,9 @@ Helper function to apply the appropriate loss function based on the specificatio | |
| """ | ||
| function _apply_loss end | ||
|
|
||
| _get_target_y(y::NamedTuple, target) = y[target] | ||
| _get_target_nan(y_nan::NamedTuple, target) = y_nan[target] | ||
|
|
||
| _get_target_y(y, target) = y(target) | ||
| _get_target_nan(y_nan, target) = y_nan(target) | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The fields
gdevandcdevare untyped in theTrainConfigstruct. In Julia, untyped fields lead to type instability, which can significantly degrade performance because the compiler cannot specialize functions using these fields. Since these are used frequently for device transfers during training, it is highly recommended to provide type annotations, such asLux.AbstractDeviceor using type parameters.