When parallel_processing within
forecast_time_series() is set to “local_machine”, each time
series (including training models on the entire data set) is ran in
parallel on the users local machine. Each time series will run on a
separate core of the machine. Hyperparameter tuning, model refitting,
and model averaging will be ran sequentially, which cannot be done in
parallel since a parallel process is already running on the machine for
each time series. This works well for data that contains many time
series where you might only want to run a few simpler models, and in
scenarios where cloud computing is not available.
If parallel_processing is set to NULL and
inner_parallel is set to TRUE within
forecast_time_series, then each time series is ran
sequentially but the hyperparameter tuning and model refitting are ran
in parallel. This works great for data that has a limited number of time
series where you want to run a lot of back testing and build dozens of
models within Finn.
With inner_parallel set to TRUE, Finn starts one pool of
worker processes, reuses it across time series and modeling steps, and
lets the tune package use it by temporarily registering it as the
future plan. Finn restores your own future
plan when the run finishes. The pool has at most num_cores
workers (by default every available core but one, respecting container
CPU limits), and never more than a modeling step can use: the
hyperparameter combinations times the validation splits, or the number
of back-test splits. Each worker needs roughly 400 MB of memory before
any data is loaded, so leave enough memory headroom when choosing
num_cores.
set_agent_info() accepts one ellmer Chat or
chat_copilot() object as an immutable configuration
template. Every iterate_forecast() time-series combo
receives an independent, empty-history session from that template,
whether combos run sequentially or in parallel. For parallel execution,
Finn sends the template through the standard foreach
serialization path and creates each session inside the series workflow
after it reaches the worker. A session persists across iterations for
its own series but is never shared with another series.
Ellmer-backed parallel runs require ellmer 0.4.0 or later in the main
process and every worker R library. The public
Chat$clone(deep = TRUE) method preserves the provider,
model, parameters, tools, and credentials without rebuilding Chat
objects through internal ellmer APIs. With Azure OpenAI API-key
authentication, the default ellmer 0.4 credential callback is serialized
with the template, so worker task payloads must be treated as sensitive.
A custom credentials callback that reads an environment
variable or obtains a token at request time requires that credential
source to be available from every worker process.
Copilot-backed workflows recreate configuration rather than cloning an ellmer Chat. Each worker needs GitHub Copilot CLI 1.0.93 or later, processx 3.9.0 or later, and available environment-token or GitHub CLI authentication. CLI version and option checks are cached in memory per process and executable; each worker checks independently, and executable changes trigger revalidation. Credentials are resolved on the worker and never serialized in the template. Copilot starts a tool-disabled process per request, replays that series’ history through stdin, and saves CLI-local state under the worker’s R temporary directory. Consider request quotas and CLI startup costs when choosing concurrency; a driver-local executable path or login is not automatically available on a Spark cluster. See the AI Agent vignette for setup and data handling requirements.
To leverage the full power of Finn, running within Azure is the best
choice in building production ready forecasts that can easily scale. The
most efficient way to run Finn is to set
parallel_processing to “spark” within
forecast_time_series(). This will run each time series in
parallel across a spark compute cluster.
Sparklyr is a great R package that allows you to run R code across a spark cluster. A user simply has to connect to a spark cluster then run Finn. Below is an example on how you can run Finn using spark on Azure Databricks. Also check out the growing R support with using spark on Azure Synapse.
# load CRAN libraries
library(finnts)
library(sparklyr)
# connect to spark cluster
options(sparklyr.log.console = TRUE)
sc <- sparklyr::spark_connect(method = "databricks")
# call Finn with spark parallel processing
hist_data <- timetk::m4_monthly %>%
dplyr::rename(Date = date) %>%
dplyr::mutate(id = as.character(id))
data_sdf <- sparklyr::copy_to(sc, hist_data, "data_sdf", overwrite = TRUE)
run_info <- set_run_info(
project_name = "finn_fcst",
run_name = "spark_run_1",
path = "/dbfs/mnt/example/folder" # important that you mount an ADLS path
)
forecast_time_series(
run_info = run_info,
input_data = data_sdf,
combo_variables = c("id"),
target_variable = "value",
date_type = "month",
forecast_horizon = 3,
parallel_processing = "spark",
return_data = FALSE
)
# return the outputs as a spark data frame
finn_output_tbl <- get_forecast_data(
run_info = run_info,
return_type = "sdf"
)The above example runs each time series on a separate core on a spark
cluster. On an Azure Synapse Spark pool, connect to the pool’s Spark
installation instead, and give set_run_info() a path under
your ADLS mount, which starts with /synfs:
sc <- sparklyr::spark_connect(
master = "yarn",
version = "3.5",
spark_home = "/opt/spark",
config = sparklyr::spark_config()
)Mount ADLS with fileCacheTimeout = 0. The default mount
caches file listings and contents for 120 seconds, so outputs that one
node just wrote can look missing or out of date on another. Finn waits
and reads such outputs again, but a mount without the cache avoids that
wait; in our Synapse tests it didn’t slow runs down.
mssparkutils.fs.mount(
"abfss://<container>@<storage account>.dfs.core.windows.net",
"/finn",
list(linkedService = "<linked service>", fileCacheTimeout = 0)
)
run_info <- set_run_info(
project_name = "finn_fcst",
run_name = "spark_run_1",
path = mssparkutils.fs.getMountPath("/finn")
)Finn reconnects to Spark at the start of each Spark step, reusing the
connection’s method and configuration. By default sparklyr then copies
the driver’s R packages to the executors again before the step’s first
task. When every executor already has the same R package library as the
driver, such as a Synapse Spark pool or a Databricks cluster with the
packages installed on the pool or cluster, you can skip that copy by
adding config$sparklyr.apply.packages <- FALSE to the
connection config. Keep the default when executors may be missing
packages, because their tasks would fail when loading Finn.
Spark 3.5 works with sparklyr 1.8.6, the version installed on Synapse Spark 3.5 pools. Spark 4 needs sparklyr 1.9.1 or later.
You can also submit multiple time series where each time series runs
on a separate spark executor (VM) and then leverage the cores on that
executor to run things like hyperparameter tuning or model refitting in
parallel. This creates two levels of parallelization. One at the time
series level, then another when doing things like hyperparameter tuning
within a specific time series. To do that set
inner_parallel to TRUE in
forecast_time_series().
Spark runs spark.executor.cores time series at once on
each executor (divided by spark.task.cpus when that is
set). It doesn’t limit the cores an R process can use, so when
num_cores is NULL, Finn gives each time series an equal
share of the executor node’s cores, minus one for the series itself. On
32-core nodes, spark.executor.cores set to 1 gives each
series about 31 inner workers, and 2 gives two series about 15 workers
each. Use 1 for heavy work within each series, such as large
hyperparameter grids, many back tests or feature selection, and 2 or
more to run more series at once. If spark.executor.cores
isn’t set (Databricks runs one series per core by default), there are no
spare cores and each series uses a single inner worker. Set it with the
“spark.executor.cores” argument when configuring your spark connection,
using sparklyr
or the cluster manager within the Azure resource. This sizing assumes
one executor per node and a driver with the same node size as the
executors; otherwise set num_cores in
forecast_time_series() to the number of inner workers each
series should use.
Inner workers run outside the Spark JVM, so they share only the node
memory Spark doesn’t reserve. For example, with
spark.executor.memory = "220g" on a 252 GB node, about 30
GB remains for all inner workers on that node.
forecast_time_series() will be looking for a variable
called “sc” to use when submitting tasks to the spark cluster, so make
sure you use that as the variable name when connecting to spark. Also
it’s important that you mount your spark session to an Azure Data Lake
Storage (ADLS) account, and provide the mounted path to where you’d like
your Finn results to be written to within
set_run_info().
Spark executors run Finn with their own R package library. Before installing a new version of Finn on a cluster, upgrade that library to the minimum package versions listed in Finn’s DESCRIPTION file, including tune 2.0.1 or later.