text:
abstract type AbstractGaussianValueBaseline <: AbstractValueBaseline end

### struct for defining a Gaussian force model
mutable struct GaussianValueBaseline <: AbstractGaussianValueBaseline
  dim::Int64
  nbasis::Int64
  shape::Tuple{Vararg{Int64}}
  mus::Vector{Vector{Float64}}
  sigs::Vector{Vector{Float64}}
  theta::Vector{Float64}
  em::Vector{Float64}
  function GaussianValueBaseline(shape::Tuple{Vararg{Int64}},mus::Vector{Vector{Float64}},sigs::Vector{Vector{Float64}},
                         theta::Vector{Float64})
    dim = length(shape)
    nbasis = reduce(*,shape,init=1)
    new(dim,nbasis,shape,mus,sigs,theta,zeros(Float64,nbasis))
  end
end

function GaussianValueBaseline(shape::Tuple{Vararg{Int64}},mus::Vector{Vector{Float64}},sigs::Vector{Vector{Float64}})
  dim::Int64 = length(shape)
  nbasis::Int64 = reduce(*,shape,init=1)
  theta::Vector{Float64} = zeros(Float64,nbasis)
  GaussianValueBaseline(shape,mus,sigs,theta)
end

"""
struct for defining a time-dependent Gaussian force model
make sure the last set of basis functions is the time-dependence
"""
mutable struct GaussianValueBaselineTD <: AbstractGaussianValueBaseline
  dim::Int64
  nbasis::Int64
  shape::Tuple{Vararg{Int64}}
  mus::Vector{Vector{Float64}}
  sigs::Vector{Vector{Float64}}
  theta::Vector{Float64}
  em::Vector{Float64}
  function GaussianValueBaselineTD(shape::Tuple{Vararg{Int64}},mus::Vector{Vector{Float64}},sigs::Vector{Vector{Float64}},
                         theta::Vector{Float64})
    dim = length(shape)-1
    nbasis = reduce(*,shape,init=1)
    new(dim,nbasis,shape,mus,sigs,theta,zeros(Float64,nbasis))
  end
end

function GaussianValueBaselineTD(shape::Tuple{Vararg{Int64}},mus::Vector{Vector{Float64}},sigs::Vector{Vector{Float64}})
  nbasis::Int64 = reduce(*,shape,init=1)
  theta::Vector{Float64} = zeros(Float64, nbasis)
  GaussianValueBaselineTD(shape,mus,sigs,theta)
end

"""
Helper functions that get useful information about the model
"""
function getdimensionality(gm::AbstractGaussianValueBaseline)
  return gm.dim
end

function gradshape(gm::AbstractGaussianValueBaseline)
  return gm.nbasis
end

"""
Functions that compute the value of the basis functions
"""
function computebasis!(sys::AbstractSystem, gm::GaussianValueBaseline)
  k::Vector{Float64} = exp.( -0.5 .* (sys.x[1] .- gm.mus[1]).^2 ./ gm.sigs[1] )
  for i in 2:gm.dim
    k = vec( k * (exp.( -0.5 .* (sys.x[i] .- gm.mus[i]).^2 ./ gm.sigs[i] ))' )
  end
  gm.em .= k
end

function computebasis!(sys::AbstractSystem, gm::GaussianValueBaselineTD)
  k::Vector{Float64} = exp.( -0.5 .* (sys.x[1] .- gm.mus[1]).^2 ./ gm.sigs[1] )
  for i in 2:gm.dim
    k = vec( k * (exp.( -0.5 .* (sys.x[i] .- gm.mus[i]).^2 ./ gm.sigs[i] ))' )
  end
  k = vec( k * (exp.( -0.5 .* (sys.t .- gm.mus[end]).^2 ./ gm.sigs[end] ))' )
  gm.em .= k
end

"""
Function that evaluates the gaussian value baseline function
"""
function callvbl(sys::AbstractSystem,vbl::AbstractGaussianValueBaseline; compbasis::Bool=true)
  if compbasis
    computebasis!(sys,vbl)
  end
  return sum( vbl.theta .* vbl.em )
end

"""
Gradient wrt parameters for gaussian model
"""
function gradient!(grad::Array{Float64},sys::AbstractSystem,gm::AbstractGaussianValueBaseline;
                   compbasis::Bool=false)
  if compbasis
    computebasis!(sys,gm)
  end
  grad .= gm.em
end

"""
Update parameters for the neural network model
"""
function updateparams!(lr::Float64,dtheta::Vector{Float64},gm::AbstractGaussianValueBaseline)
    # update coefficients for forces
    gm.theta .+= lr .* dtheta
end

"""
Set parameters for the neural network model
"""
function setparams!(theta::Vector{Float64},gm::AbstractGaussianValueBaseline)
    # update coefficients for forces
    gm.theta .= theta
end

function setparams!(gm::GaussianModel)
end

mutable struct GaussianValueBaseline2D <: AbstractValueBaseline
  nx::Int64
  ny::Int64
  nt::Int64
  mux::Vector{Float64}
  muy::Vector{Float64}
  mut::Vector{Float64}
  sigx::Vector{Float64}
  sigy::Vector{Float64}
  sigt::Vector{Float64}
  theta::Vector{Float64}
  em::Vector{Float64}
  function GaussianValueBaseline2D(nx::Int64,ny::Int64,nt::Int64,
                                   mux::Vector{Float64},muy::Vector{Float64},mut::Vector{Float64},
                                   sigx::Vector{Float64},sigy::Vector{Float64},sigt::Vector{Float64},
                                   theta::Vector{Float64})
    new(nx,ny,nt,mux,muy,mut,sigx,sigy,sigt,theta,zeros(Float64,size(theta)))
  end
end

function gradshape(vbl::GaussianValueBaseline2D)
  return size(vbl.theta)
end

function callvbl(sys::AbstractSystem,vbl::GaussianValueBaseline2D)
  vbl.em .= vec(kron(exp.(-0.5 .* (sys.t .- vbl.mut).^2 ./ vbl.sigt),
                kron(exp.(-0.5 .* (sys.x[2] .- vbl.muy).^2 ./ vbl.sigy),
                     exp.(-0.5 .* (sys.x[1] .- vbl.mux).^2 ./ vbl.sigx))))
  return sum( vbl.theta.*vbl.em )
end

"""
Gradient wrt parameters for gaussian value baseline
"""
function gradient!(grad::Array{Float64},sys::AbstractSystem,vbl::GaussianValueBaseline2D)
  grad .= vbl.em
end

"""
Update parameters for the neural network model
"""
function updateparams!(lr::Float64,dtheta::Vector{Float64},vbl::GaussianValueBaseline2D)
    # update coefficients for forces
    vbl.theta .+= (lr .* dtheta)
end

"""
Set parameters for the neural network model
"""
function setparams!(theta::Vector{Float64},vbl::GaussianValueBaseline2D)
    # update coefficients for forces
    vbl.theta .= theta
end

function setparams!(gm::GaussianValueBaseline2D)
end


text:
# Primal infeasible test problems
# Problems with m1 feasible equality constraints, and one infeasible psd constraint
# x >= 0, elements in A >=0, elements in b < 0 and s=[s1,s2,s3] with s1 in {0}, s2 in {R+} and s3 in psd-cone
# s3 cannot be found since Ax > 0 and b < 0 (Ax + s = b)

nn = 1
rng = Random.MersenneTwister(1313)

@testset "Primal infeasible QP problems - Testset 2" begin
  for iii = 1:nn

    # choose size of problem
    n = rand(rng, 10:50)
    r = rand(rng, 2:10)
    m2 = r^2 # one sdp cone constraint of size r
    m1 = m2 #m1 equality constraints
    m = m1+m2
    A = sprand(rng,m,n,0.8)*50
    xtrue = rand(rng,n,1)*50
    s1 = zeros(m1,1)
    b1 = A[1:m1,:]*xtrue + s1 #find feasible part of b that corresponds to the equality constraints
    b3 = -rand(m2,1) #pick vectorized matrix with all entries <= 0 --> to enforce infeasibility

    # add the constraint x >= 0
    b = [b1;zeros(n);b3]
    b = vec(b)
    A1 = -A[1:m1,:]
    A2 = sparse(1.0I,n,n)
    A3 = -A[m1+1:end,:]
    b1 = b1
    b2 = zeros(n)
    b3 = b3
    A = [A[1:m1,:]; -sparse(1.0I,n,n);A[m1+1:end,:]]

    # b2 = zeros(n)
    # b3 =
    # create dual feasible problem Px+q+A'y = 0, and y in K*
    P = generate_pos_def_matrix(n,rng)
    ytrue_1 = randn(rng,m1,1)*50
    ytrue_2 = rand(rng,n,1)*50
    ytrue_3 = vec(generate_pos_def_matrix(r,rng, MT = UnitTestFloat))
    ytrue = [ytrue_1;ytrue_2;ytrue_3]
    q = (-P*xtrue - A'*ytrue)[:]

    Kf = m1
    Kl = n
    Kq = []
    Ks = [r^2]
    cs1 = COSMO.Constraint(A1,b1,COSMO.ZeroSet)
    cs2 = COSMO.Constraint(A2,b2,COSMO.Nonnegatives)
    cs3 = COSMO.Constraint(A3,b3,COSMO.PsdCone)

    settings = COSMO.Settings(max_iter=10000, eps_abs = 1e-5, eps_rel=1e-5)

    model = COSMO.Model()
    assemble!(model,P,q,[cs1;cs2;cs3], settings = settings)
    res = COSMO.optimize!(model);

    @test res.status == :Primal_infeasible
  end
end
nothing

ext: jl


text:
# rk4.jl
# Runge Kutta 4th order solver for ODEs

export rk4

# base RK4 method:
# dxdt = f(t,x)

# Inputs:
#   f:      function, that accepts input: (scalar t, vector x_old, vector x_new)
#   h:      delta t
#   x_ic:   initial condition for x
#   t_max:  length of time to step through
# Outputs:
#   x:      solved x at t_max

"""
  This type stores all the data RK4 needs to restart.  It is a subtype of
  [`AbstractCheckpointData`](@ref Utils.AbstractCheckpointData).

  **Fields**

   * i: the current time step

"""
mutable struct RK4CheckpointData <: AbstractCheckpointData
  i::Int  # current time step
end

"""
  This constructor loads a RK4CheckpointData from the most recently saved
  checkpoint.

  **Inputs**

   * chkpointer: a [`Checkpointer`](@ref Utils.Checkpointer)
   * comm_rank: MPI rank of this process

  **Outputs**

   * chkpoint_data: a RK4CheckpointData object
"""
function RK4CheckpointData(chkpointer::Checkpointer, comm_rank::Integer)

  chkpoint_data = readLastCheckpointData(chkpointer, comm_rank)

  return chkpoint_data::RK4CheckpointData
end

"""
  This function assists in setting up checkpoinging related things for
  self-starting explicit time marching methods

  **Inputs**

   * opts: the options dictionary
   * myrank: MPI rank of this process

  **Outputs*

   * chkpointer: a Checkpointer fully initialized
   * chkpointdata: a RK4CheckpoinntData object, fully initialized
   * skip_checkpoint: a bool indicating if the next checkpoint write should be
                      skipped
"""
function explicit_checkpoint_setup(opts, myrank)
  is_restart = opts["is_restart"]
  ncheckpoints = opts["ncheckpoints"]

  if !is_restart
    # this is a new simulation, create all the stuff needed to checkpoint
    # note that having zero checkpoints is valid
    istart = 2
    chkpointdata = RK4CheckpointData(istart)
    chkpointer = Checkpointer(myrank, ncheckpoints)
    skip_checkpoint = false
  else  # this is a restart, load existing data
    # using default value of 0 checkpoints is ok
    chkpointer = Checkpointer(opts, myrank)
    chkpointdata = RK4CheckpointData(chkpointer, myrank)
    skip_checkpoint = true  # when restarting, don't immediately write a
                            # checkpoint
                            # doing so it not strictly incorrect, but not useful
  end

  return chkpointer, chkpointdata, skip_checkpoint
end

@doc """
rk4

  This function does 4th order Runge Kutta time stepping, using a function of
  the form du/dt = f(u, t)

  Arguments:
    * f  : function evaluation, must have signature (ctx..., opts, t)
    * h  : time step size
    * t_max: time value to stop time stepping (time starts at 0)
    * q_vec: vector of the u values, must be eqn.q_vec
    * res_vec: vector of du/dt values (the output of the function f)
    * pre_func: function to to be called after the new u values are put into
                q_vec but before the function f is evaluated.  Must have
                signature: pre_func(ctx..., opts)
    * post_func: function called immediately after f is called.  The function
                 must have the signature res_norm = post_func(ctx..., opts,
                 calc_norm=true),
                 where res_norm is a norm of res_vec, and calc_norm determines
                 whether or not to calculate the norm.
    * ctx: a tuple (or any iterable container) of the objects needed by
           f, pre_func, and post func.  The tuple is splatted before being
           passed to the functions.
    * opts : options dictionary
    * timing: a Timing object, a new one will be created if not provided

    Keyword Arguments:
    * majorIterationCallback: a callback function called after the first
                              stage, useful to do output or logging
    * res_tol : keyword arg, residual topping tolerance
    * real_time : do actual time marching, not pseudo-time marching

   The eqn.q_vec should hold the whichever variables (conservative or
   entropy) that the simulation should use.

   The idea behind pre_func, post func, and ctx is that they abstract what kind
   of system rk4 is timestepping.  rk4 only needs to know about q_vec and
   res_vec.

   For physics modules, ctx should be (mesh, sbp, eqn) and q_vec and res_vec
   should be eqn.q_vec and eqn.res_vec.  For physics modules, pre_func should
   take the values from q_vec and put them in q, and post_func should take
   the values in res and put them in res_vec.  Thus pre_func and post_func
   provide the link between the way the rk4 represents the data and the
   way the physics modules represent the data.

   Options Keys

   Implementation Notes
     sol_norm check is only performed in real_time mode
"""->
function rk4(f::Function, h::AbstractFloat, t_max::AbstractFloat,
             q_vec::AbstractVector, res_vec::AbstractVector, pre_func,
             post_func, ctx, opts, timing::Timings=Timings();
             majorIterationCallback=((a...) -> (a...)),
             res_tol = -1.0, real_time=false)

  myrank = MPI.Comm_rank(MPI.COMM_WORLD)  #???
  if myrank == 0
    println(BSTDOUT, "\nEntered rk4")
    println(BSTDOUT, "res_tol = ", res_tol)
  end
# res_tol is alternative stopping criteria

  # unpack options
  output_freq = opts["output_freq"]::Int
  use_itermax = opts["use_itermax"]::Bool
  if use_itermax
    itermax = opts["itermax"]
  end

  use_checkpointing = opts["use_checkpointing"]::Bool
  chkpoint_freq = opts["checkpoint_freq"]::Int
  ncheckpoints = opts["ncheckpoints"]::Int

  t = 0.0  # timestepper time
  treal = 0.0  # real time (as opposed to pseudo-time)
  t_steps = round(Int, t_max/h)
  @mpi_master println(BSTDOUT, "t_steps: ",t_steps)
  @mpi_master println(BSTDOUT, "delta_t = ", h)

  (m,) = size(q_vec)

  if myrank == 0
    _f1 = open("convergence.dat", "a")
    f1 = BufferedIO(_f1)
  end

  x_old = copy(q_vec)
  k1 = zeros(x_old)
  k2 = zeros(x_old)
  k3 = zeros(x_old)
  k4 = zeros(x_old)

  # Note: q_vec_old_DEBUG is a tool for showing the change in q between timesteps for comparison with CN (for ex)
#   q_vec_old_DEBUG = zeros(q_vec)

  # setup all the checkpointing related data
  chkpointer, chkpointdata, skip_checkpoint = explicit_checkpoint_setup(opts, myrank)
  istart = chkpointdata.i

  flush(BSTDOUT)
  #-----------------------------------------------------
  ### Main timestepping loop ###
  # beginning of RK4 time stepping loop
  #TODO: make this loop 1-based
  # this loop is 2:(t_steps + 1) when not restarting
  timing.t_timemarch += @elapsed for i=istart:(t_steps + 1)

    # compute time value from time step
    t = (i - 2)*h

#     q_vec_old_DEBUG = deepcopy(q_vec)

    @mpi_master if i % output_freq == 0
       println(BSTDOUT, "\ntimestep ",i)
    end

    if use_checkpointing && i % chkpoint_freq == 0
      if skip_checkpoint    # skips only the first checkpoint
        skip_checkpoint = false
      else

        @mpi_master println(BSTDOUT, "Saving checkpoint at timestep ", i)
        # save all needed variables to the chkpointdata
        chkpointdata.i = i

        if countFreeCheckpoints(chkpointer) == 0
          freeOldestCheckpoint(chkpointer)  # make room for a new checkpoint
        end

        # save the checkpoint
        saveNextFreeCheckpoint(chkpointer, ctx..., opts, chkpointdata)

      end   # end of if skip_checkpoint check
    end   # end of if use_checkpointing check

    # flush after all printing
    if i % output_freq == 0
      flush(BSTDOUT)
    end

    # stage 1
    pre_func(ctx..., opts)
    if real_time treal = t end
    timing.t_func += @elapsed f( ctx..., opts, treal)
    sol_norm = post_func(ctx..., opts)

    timing.t_callback += @elapsed majorIterationCallback(i, ctx..., opts, BSTDOUT)
    for j=1:m
      k1[j] = res_vec[j]
      q_vec[j] = x_old[j] + (h/2)*k1[j]
    end

    # logging
    @mpi_master if i % 1 == 0
      println(f1, i, " ", sol_norm)
    end

    @mpi_master if i % output_freq == 0
      println(BSTDOUT, "flushing convergence.dat to disk")
      flush(f1)
    end

    # check stopping conditions
    if (sol_norm < res_tol) && !real_time
      if myrank == 0
        println(BSTDOUT, "breaking due to res_tol, res norm = $sol_norm")
        close(f1)
        flush(BSTDOUT)
      end
      break
    end

    if use_itermax && i > itermax
      if myrank == 0
        println(BSTDOUT, "breaking due to itermax")
        close(f1)
        flush(BSTDOUT)
      end
      break
    end

    # stage 2
    pre_func(ctx..., opts)
    if real_time  treal = t + h/2 end
    timing.t_func += @elapsed f( ctx..., opts, treal)
    post_func(ctx..., opts, calc_norm=false)
    for j=1:m
      k2[j] = res_vec[j]
      q_vec[j] = x_old[j] + (h/2)*k2[j]
    end

    # stage 3
    pre_func(ctx..., opts)
    if real_time treal= t + h/2 end
    timing.t_func += @elapsed f( ctx..., opts, treal)
    post_func(ctx..., opts, calc_norm=false)

    for j=1:m
      k3[j] = res_vec[j]
      q_vec[j] = x_old[j] + h*k3[j]
    end

    # stage 4
    pre_func(ctx..., opts)
    if real_time treal = t + h end
    timing.t_func += @elapsed f( ctx..., opts, treal)
    post_func(ctx..., opts, calc_norm=false)
    for j=1:m
      k4[j] = res_vec[j]
    end

#     println("k1 = \n", k1)
#     println("k2 = \n", k2)
#     println("k3 = \n", k3)
#     println("k4 = \n", k4)

#     println("q_old = \n", x_old)

    # update
    for j=1:m
      x_old[j] = x_old[j] + (h/6)*(k1[j] + 2*k2[j] + 2*k3[j] + k4[j])
      q_vec[j] = x_old[j]
    end

#     println("q_vec = \n", q_vec)

    #TODO: is this necessary?
#    fill!(k1, 0.0)
#    fill!(k2, 0.0)
#    fill!(k3, 0.0)
#    fill!(k4, 0.0)

#    t = t + h

  end   # end of RK4 time stepping loop

  t += h  # final time step

  if myrank == 0
    close(f1)
  end

  flush(BSTDOUT)

  # should this be treal?
  return t

end

# this is the version for solving PDEs
# it uses the pde_pre_func and pde_post_func below
@doc """
### NonlinearSolvers.rk4

  This method of rk4 takes in the ctx, but not the pre_func and post_func,
  using pde_pre_func and pde_post_func instead.

  All argument names are the same as for the main rk4 method

  Inputs:
    f:
    h
    t_max
    q_vec
    res_vec
    ctx
    opts
    majorIterationCallback
    res_tol
    real_time
"""->
function rk4(f::Function, h::AbstractFloat, t_max::AbstractFloat,
             q_vec::AbstractVector, res_vec::AbstractVector, ctx, opts, timing::Timings=Timings();
             majorIterationCallback=((a...) -> (a...)), res_tol=-1.0,
             real_time=false)

    rk4(f::Function, h::AbstractFloat, t_max::AbstractFloat, q_vec::AbstractVector,
        res_vec::AbstractVector, pde_pre_func, pde_post_func, ctx, opts;
        majorIterationCallback=majorIterationCallback, res_tol =res_tol, real_time=real_time)

end

@doc """
### NonlinearSolvers.rk4

  This is the original (non-general) interface for rk4.

  The argument names are the same as for the main rk4 method, unless noted
  otherwise

  Inputs:
    f
    h
    t_max
    mesh:  mesh object
    sbp: sbp object
    eqn: equation object
    opts

  Keyword Arguments:
    res_tol
    real_time

  eqn.q_vec for q_vec, eqn.res_vec for res_vec, pde_pre_func and pde_post func
  for the pre and post functions, eqn.majorIterationCallback for the
  majorIterationCallback, and (mesh, sbp, eqn) as the ctx
"""->
function rk4(f::Function, h::AbstractFloat, t_max::AbstractFloat, mesh, sbp, eqn, opts; res_tol=-1.0, real_time=false)

  rk4(f, h, t_max, eqn.q_vec, eqn.res_vec, pde_pre_func, pde_post_func,
      (mesh, sbp, eqn), opts, eqn.params.time;
      majorIterationCallback=eqn.majorIterationCallback, res_tol=res_tol, real_time=real_time)

end

@doc """
### NonlinearSolvers.pde_pre_func

  The pre-function for solving partial differential equations with a physics
  module.  The only operation it performs is disassembling eqn.q_vec into
  eqn.q

  Inputs:
    mesh
    sbp
    eqn
    opts
"""->
function pde_pre_func(mesh, sbp, eqn, opts)

  array1DTo3D(mesh, sbp, eqn, opts, eqn.q_vec, eqn.q)
end

@doc """
### NonlinearSolvers.pde_post_func

  The post-function for solving partial differential equations with a physics
  module.  This function multiplies by A0inv, assembles eqn.res into
  eqn.res_vec, multiplies by the inverse mass matrix, and calculates
  the SBP approximation to the integral L2 norm

  Inputs:
    mesh
    sbp
    eqn
    opts

"""->
function pde_post_func(mesh, sbp, eqn, opts; calc_norm=true)
  eqn.multiplyA0inv(mesh, sbp, eqn, opts, eqn.res)
  array3DTo1D(mesh, sbp, eqn, opts, eqn.res, eqn.res_vec)
  for j=1:length(eqn.res_vec) eqn.res_vec[j] = eqn.Minv[j]*eqn.res_vec[j] end
  if calc_norm
    local_norm = calcNorm(eqn, eqn.res_vec)
    eqn.params.time.t_allreduce += @elapsed global_norm = MPI.Allreduce(local_norm*local_norm, MPI.SUM, mesh.comm)
    return sqrt(global_norm)
  end

  return nothing
end

#DEBUGGING

function globalNorm(vec)

  local_norm = norm(vec)
  global_norm = MPI.Allreduce(local_norm*local_norm, MPI.SUM, MPI.COMM_WORLD)
  return sqrt(global_norm)
end


text:
# On vérifie que le constructeur par défaut fonctionne
ls_at_t = LSAtT(0.0)

@test ls_at_t.x == 0.0
@test isnan(ls_at_t.ht)
@test isnan(ls_at_t.gt)
@test isnan(ls_at_t.h₀)
@test isnan(ls_at_t.g₀)
@test isnan(ls_at_t.start_time)

# On test la fonction update!(...)
update!(ls_at_t, x = 1.0, ht = 1.0, gt = 1.0, h₀ = 1.0)
update!(ls_at_t, g₀ = 1.0, tmps = 0.0)

@test ls_at_t.x == 1.0
@test ls_at_t.ht == 1.0
@test ls_at_t.gt == 1.0
@test ls_at_t.h₀ == 1.0
@test ls_at_t.g₀ == 1.0
@test ls_at_t.start_time == 0.0

# on vérifie que la fonction copy fonctionne
ls_at_t_2 = copy(ls_at_t)

@test ls_at_t_2.x == 1.0
@test ls_at_t_2.ht == 1.0
@test ls_at_t_2.gt == 1.0
@test ls_at_t_2.h₀ == 1.0
@test ls_at_t_2.g₀ == 1.0
@test ls_at_t_2.start_time == 0.0

ls_64 = LSAtT(0.0)
update!(ls_64, x = 1.0, ht = 1.0, gt = 1.0, h₀ = 1.0)

ls_32 = convert_ls(Float32, ls_64)
@test typeof(ls_32.x) == Float32


text:
# This file is a part of Julia. License is MIT: http://julialang.org/license

using HTTPClient.HTTPC

env_name = chomp(readstring(`hostname`))
commit = Base.GIT_VERSION_INFO.commit
flavor = ENV["JULIA_FLAVOR"]
json = "{\"env\": \"$env_name\", \"blas\":\"$flavor\", \"commit\":\"$commit\"}"
post("http://status.julialang.org/put/codespeed", json )


text:
abstract type AbstractReservoir end

function get_ressize(reservoir::AbstractReservoir)
    return reservoir.res_size
end

function get_ressize(reservoir)
    return size(reservoir, 1)
end

struct RandSparseReservoir{T,C} <: AbstractReservoir
    res_size::Int
    radius::T
    sparsity::C
end

"""
    RandSparseReservoir(res_size, radius, sparsity)
    RandSparseReservoir(res_size; radius=1.0, sparsity=0.1)

Returns a random sparse reservoir initializer, that will return a matrix with given
`sparsity` and scaled spectral radius according to `radius`. This is the default choice
in the ```ESN``` construction.
"""
function RandSparseReservoir(res_size; radius=1.0, sparsity=0.1)
    return RandSparseReservoir(res_size, radius, sparsity)
end

"""
    create_reservoir(reservoir::AbstractReservoir, res_size)
    create_reservoir(reservoir, args...)

Given an ```AbstractReservoir` constructor and the reservoir size it returns the
corresponding matrix. Alternatively it accepts a given matrix.
"""
function create_reservoir(
    reservoir::RandSparseReservoir,
    res_size;
    matrix_type=Matrix{Float64}
)
    reservoir_matrix = Matrix(sprand(res_size, res_size, reservoir.sparsity))
    reservoir_matrix = 2.0 .*(reservoir_matrix.-0.5)
    replace!(reservoir_matrix, -1.0=>0.0)
    rho_w = maximum(abs.(eigvals(reservoir_matrix)))
    reservoir_matrix .*= reservoir.radius/rho_w
    #TODO: change to explicit if
    Inf in unique(reservoir_matrix) || -Inf in unique(reservoir_matrix) ? error(
        "Sparsity too low for size of the matrix.
        Increase res_size or increase sparsity") : nothing
    return Adapt.adapt(matrix_type, reservoir_matrix)
end

function create_reservoir(reservoir, args...; kwargs...)
    return reservoir
end

#=
function create_reservoir(res_size, reservoir::RandReservoir)
    sparsity = degree/res_size
    W = Matrix(sprand(Float64, res_size, res_size, sparsity))
    W = 2.0 .*(W.-0.5)
    replace!(W, -1.0=>0.0)
    rho_w = maximum(abs.(eigvals(W)))
    W .*= radius/rho_w
    W
end
=#

struct PseudoSVDReservoir{T,C} <: AbstractReservoir
    res_size::Int
    max_value::T
    sparsity::C
    sorted::Bool
    reverse_sort::Bool
end

function PseudoSVDReservoir(
    res_size;
    max_value=1.0,
    sparsity=0.1,
    sorted=true,
    reverse_sort=false
)
    return PseudoSVDReservoir(res_size, max_value, sparsity, sorted, reverse_sort)
end

"""
    PseudoSVDReservoir(max_value, sparsity, sorted, reverse_sort)
    PseudoSVDReservoir(max_value, sparsity; sorted=true, reverse_sort=false)

Returns an initializer to build a sparse reservoir matrix, with given ```sparsity```
created using SVD as described in [1].

[1] Yang, Cuili, et al. "_Design of polynomial echo state networks for time
series prediction._" Neurocomputing 290 (2018): 148-160.
"""
function PseudoSVDReservoir(res_size, max_value, sparsity; sorted=true, reverse_sort=false)
    return PseudoSVDReservoir(res_size, max_value, sparsity, sorted, reverse_sort)
end

function create_reservoir(
    reservoir::PseudoSVDReservoir,
    res_size;
    matrix_type=Matrix{Float64}
)
    sorted = reservoir.sorted
    reverse_sort = reservoir.reverse_sort
    reservoir_matrix = create_diag(
        res_size, reservoir.max_value, sorted=sorted, reverse_sort=reverse_sort
    )
    tmp_sparsity = get_sparsity(reservoir_matrix, res_size)

    while tmp_sparsity <= reservoir.sparsity
        reservoir_matrix *= create_qmatrix(
            res_size, rand(1:res_size), rand(1:res_size), rand()*2-1
        )
        tmp_sparsity = get_sparsity(reservoir_matrix, res_size)
    end

    return Adapt.adapt(matrix_type, reservoir_matrix)
end

function create_diag(dim, max_value; sorted = true, reverse_sort = false)
    diagonal_matrix = zeros(dim, dim)
    if sorted == true
        if reverse_sort == true
            diagonal_values = sort(rand(dim).*max_value, rev = true)
            diagonal_values[1] = max_value
        else
            diagonal_values = sort(rand(dim).*max_value)
            diagonal_values[end] = max_value
        end
    else
        diagonal_values = rand(dim).*max_value
    end

    for i in 1:dim
        diagonal_matrix[i, i] = diagonal_values[i]
    end

    return diagonal_matrix
end

function create_qmatrix(dim, coord_i, coord_j, theta)
    qmatrix = zeros(dim, dim)

    for i in 1:dim
        qmatrix[i,i] = 1.0
    end

    qmatrix[coord_i, coord_i] = cos(theta)
    qmatrix[coord_j, coord_j] = cos(theta)
    qmatrix[coord_i, coord_j] = -sin(theta)
    qmatrix[coord_j, coord_i] = sin(theta)
    return qmatrix
end

function get_sparsity(M, dim)
    return size(M[M .!= 0], 1)/(dim*dim-size(M[M .!= 0], 1)) #nonzero/zero elements
end

#from "minimum complexity echo state network" Rodan
# Delay Line Reservoir

struct DelayLineReservoir{T} <: AbstractReservoir
    res_size::Int
    weight::T
end

"""
    DelayLineReservoir(res_size, weight)
    DelayLineReservoir(res_size; weight=0.1)

Returns a Delay Line Reservoir matrix constructor to obtain a deterministi reservoir as
described in [1]. The ```weight``` can be passed as arg or kwarg and it determines the
absolute value of all the connections in the reservoir.

[1] Rodan, Ali, and Peter Tino. "_Minimum complexity echo state network._"
IEEE transactions on neural networks 22.1 (2010): 131-144.
"""
function DelayLineReservoir(res_size; weight=0.1)
    return DelayLineReservoir(res_size, weight)
end

function create_reservoir(
    reservoir::DelayLineReservoir,
    res_size;
    matrix_type=Matrix{Float64}
)
    reservoir_matrix = zeros(res_size, res_size)

    for i in 1:res_size-1
        reservoir_matrix[i+1, i] = reservoir.weight
    end

    return Adapt.adapt(matrix_type, reservoir_matrix)
end

#from "minimum complexity echo state network" Rodan
# Delay Line Reservoir with backward connections
struct DelayLineBackwardReservoir{T} <: AbstractReservoir
    res_size::Int
    weight::T
    fb_weight::T
end

"""
    DelayLineBackwardReservoir(res_size, weight, fb_weight)
    DelayLineBackwardReservoir(res_size; weight=0.1, fb_weight=0.2)

Returns a Delay Line Reservoir constructor to create a matrix with Backward connections
as described in [1]. The ```weight``` and ```fb_weight``` can be passed as either args or
kwargs, and they determine the only absolute values of the connections in the reservoir.

[1] Rodan, Ali, and Peter Tino. "_Minimum complexity echo state network._"
IEEE transactions on neural networks 22.1 (2010): 131-144.
"""
function DelayLineBackwardReservoir(res_size; weight=0.1, fb_weight=0.2)
    return DelayLineBackwardReservoir(res_size, weight, fb_weight)
end

function create_reservoir(
    reservoir::DelayLineBackwardReservoir,
    res_size;
    matrix_type=Matrix{Float64}
)
    reservoir_matrix = zeros(res_size, res_size)

    for i in 1:res_size-1
        reservoir_matrix[i+1, i] = reservoir.weight
        reservoir_matrix[i, i+1] = reservoir.fb_weight
    end

    return Adapt.adapt(matrix_type, reservoir_matrix)
end

#from "minimum complexity echo state network" Rodan
# Simple cycle reservoir
struct SimpleCycleReservoir{T} <: AbstractReservoir
    res_size::Int
    weight::T
end

"""
    SimpleCycleReservoir(res_size, weight)
    SimpleCycleReservoir(res_size; weight=0.1)

Returns a Simple Cycle Reservoir Reservoir constructor to biuld a reservoir matrix as
described in [1]. The ```weight``` can be passed as arg or kwarg and it determines the
absolute value of all the connections in the reservoir.

[1] Rodan, Ali, and Peter Tino. "Minimum complexity echo state network."
IEEE transactions on neural networks 22.1 (2010): 131-144.
"""
function SimpleCycleReservoir(res_size; weight=0.1)
    return SimpleCycleReservoir(res_size, weight)
end

function create_reservoir(
    reservoir::SimpleCycleReservoir,
    res_size;
    matrix_type=Matrix{Float64})
    reservoir_matrix = zeros(Float64, res_size, res_size)

    for i in 1:res_size-1
        reservoir_matrix[i+1, i] = reservoir.weight
    end

    reservoir_matrix[1, res_size] = reservoir.weight
    return Adapt.adapt(matrix_type, reservoir_matrix)
end

#from "simple deterministically constructed cycle reservoirs with regular jumps" by Rodan and Tino
# Cycle Reservoir with Jumps
struct CycleJumpsReservoir{T} <: AbstractReservoir
    res_size::Int
    cycle_weight::T
    jump_weight::T
    jump_size::Int
end

"""
    CycleJumpsReservoir(res_size; cycle_weight=0.1, jump_weight=0.1, jump_size=3)
    CycleJumpsReservoir(res_size, cycle_weight, jump_weight, jump_size)

Return a Cycle Reservoir with Jumps constructor to create a reservoir matrix as described
in [1]. The ```weight``` and ```jump_weight``` can be passed as args or kwargs and they
determine the absolute values of all the connections in the reservoir. The ```jump_size```
can also be passed either as arg and kwarg and it detemines the jumps between
```jump_weight```s.

[1] Rodan, Ali, and Peter Tiňo. "_Simple deterministically constructed cycle reservoirs
with regular jumps._" Neural computation 24.7 (2012): 1822-1852.
"""
function CycleJumpsReservoir(res_size; cycle_weight=0.1, jump_weight=0.1, jump_size=3)
    return CycleJumpsReservoir(res_size, cycle_weight, jump_weight, jump_size)
end

function create_reservoir(
    reservoir::CycleJumpsReservoir,
    res_size;
    matrix_type=Matrix{Float64}
)
    reservoir_matrix = zeros(res_size, res_size)

    for i in 1:res_size-1
        reservoir_matrix[i+1, i] = reservoir.cycle_weight
    end

    reservoir_matrix[1, res_size] = reservoir.cycle_weight

    for i in 1:reservoir.jump_size:res_size-reservoir.jump_size
        tmp = (i+reservoir.jump_size)%res_size
        if tmp == 0
            tmp = res_size
        end
        reservoir_matrix[i, tmp] = reservoir.jump_weight
        reservoir_matrix[tmp, i] = reservoir.jump_weight
    end

    return Adapt.adapt(matrix_type, reservoir_matrix)
end

"""
    NullReservoir()

Return a constructor for a matrix `zeros(res_size, res_size)`
"""
struct NullReservoir <: AbstractReservoir end

function create_reservoir(
    reservoir::NullReservoir,
    res_size;
    matrix_type=Matrix{Float64})
    return Adapt.adapt(matrix_type, zeros(res_size, res_size))
end

max_stars_repo_name: MartinuzziFrancesco/EchoStateNetwork

max_issues_repo_name: MartinuzziFrancesco/EchoStateNetwork

max_forks_repo_name: MartinuzziFrancesco/EchoStateNetwork


text:
using Documenter
using Gomen

DocMeta.setdocmeta!(Gomen, :DocTestSetup, :(using Gomen); recursive=true)
makedocs(
    sitename = "Gomen",
    format = Documenter.HTML(),
    modules = [Gomen],
    authors = "Douglas G. Moore",
    pages = Any[
        "Home" => "index.md",
        "API Reference" => "api.md",
    ]
)

deploydocs(
    repo = "github.com/ELIFE-ASU/Gomen.jl.git"
)


text:
__precompile__(true)

module Binary_BB_Trees

using DocStringExtensions
# using Printf
# using GeometryTypes: HomogenousMesh
using Rotations
using LinearAlgebra
using StaticArrays
# using DataStructures
# using Statistics
# using NearestNeighbors
using ..MathKernel

include("box_types.jl")
# include("mesh.jl")
include("util.jl")
include("vector_cache.jl")
include("tree_types.jl")
include("bb_intersection.jl")
# include("blob_types.jl")
# include("top_down.jl")
include("obb_construction.jl")
include("extensions.jl")

export
    # box_types.jl
    BoundingBox,
    OBB,

    # util.jl
    calc_min_max,
    calc_obb,
    sort_so_big_ϵ_last,

    # vector_cache.jl
    VectorCache,
    expand!,
    returnNext,
    addCacheItem!,

    # tree_types.jl
    bin_BB_Tree,
    TT_Cache,
    update_TT_Cache!,
    is_leaf,
    is_not_leaf,
    treeDepth,
    leafNumber,
    tree_tree_intersect,
    extractData,

    # bb_intersection.jl
    BB_BB_intersect,

    # obb_construction.jl
    fit_tet_obb,
    fit_tri_obb

    # extensions.jl

end # module


text:
export MusicTransformerBlock, PositionEncoding

using Flux: @functor
using Tullio

using Transformers.Basic
using Transformers.Basic: AbstractTransformer, AbstractAttention, MultiheadAttention, PwFFN
using Transformers: Abstract3DTensor, batchedmul

struct MusicTransformerBlock{MA<:MultiheadAttention, LA<:LayerNorm, P<:PwFFN, LP<:LayerNorm, DP<:Dropout} <: AbstractTransformer
    mh::MA
    mhn::LA
    pw::P
    pwn::LP
    drop::DP
end

@functor MusicTransformerBlock

"""
    MusicTransformerBlock(size::Int, head::Int, ps::Int;
                          future::Bool = true, act = relu, pdrop = 0.1)
    MusicTransformerBlock(size::Int, head::Int, hs::Int, ps::Int;
                          future::Bool = true, act = relu, pdrop = 0.1)

Return a Pre-LayerNorm Transformer Encoder.
"""
function MusicTransformerBlock(size::Int, head::Int, ps::Int; future::Bool = true, act = relu, pdrop = 0.1)
    rem(size, head) != 0 && error("size not divisible by head")
    MusicTransformerBlock(size, head, div(size, head), ps;future=future, act=act, pdrop=pdrop)
end

MusicTransformerBlock(size::Int, head::Int, hs::Int, ps::Int; future::Bool = true, act = relu, pdrop = 0.1) = MusicTransformerBlock(
    MultiheadAttention(head, size, hs, size; future=future, pdrop=pdrop),
    LayerNorm(size),
    PwFFN(size, ps, act),
    LayerNorm(size),
    Dropout(pdrop),
)

function (t::MusicTransformerBlock)(x::A, mask=nothing) where {T, N, A<:AbstractArray{T, N}}
    dropout = t.drop

    # Layer norm is a preprocess for the unconditional Music Transformer,
    # Dropout and residual connection are postprocesses

    # Self-Attention layer
    x_normed = t.mhn(x)
    a = t.mh(x_normed, x_normed, x_normed; mask=mask)
    a = dropout(a)
    res_a = x + a

    # Feed-forward layer
    res_a_normed = t.pwn(res_a)
    pwffn = t.pw(res_a_normed)
    pwffn = dropout(pwffn)
    res_pwffn = res_a + pwffn
    res_pwffn
end

function Base.show(io::IO, t::MusicTransformerBlock)
    hs = div(size(t.mh.iqproj.W)[1], t.mh.head)
    h, ps = size(t.pw.dout.W)

    print(io, "MusicTransformerBlock(")
    print(io, "head=$(t.mh.head), ")
    print(io, "head_size=$(hs), ")
    print(io, "pwffn_size=$(ps), ")
    print(io, "size=$(h)")
    if Flux.istraining()
        print(io, ", dropout=$(t.drop.p))")
    else
        print(io, ")")
    end
end

struct MusicTransformerDecoder{MA<:MultiheadAttention, LA<:LayerNorm,
                               IMA<:MultiheadAttention, ILA<:LayerNorm,
                               P<:PwFFN, LP<:LayerNorm, DP<:Dropout} <: AbstractTransformer
    mh::MA
    mhn::LA
    imh::IMA
    imhn::ILA
    pw::P
    pwn::LP
    drop::DP
end

@functor MusicTransformerDecoder

"""
    MusicTransformerDecoder(size::Int, head::Int, ps::Int;
                            act = relu, pdrop = 0.1)
    MusicTransformerDecoder(size::Int, head::Int, hs::Int, ps::Int;
                            act = relu, pdrop = 0.1)

Return a Pre-LayerNorm Transformer Decoder.
"""
function MusicTransformerDecoder(size::Int, head::Int, ps::Int; act = relu, pdrop = 0.1)
    rem(size, head) != 0 && error("size not divisible by head")
    MusicTransformerDecoder(size, head, div(size, head), ps; act=act, pdrop=pdrop)
end

MusicTransformerDecoder(size::Int, head::Int, hs::Int, ps::Int; act = relu, pdrop = 0.1) = MusicTransformerDecoder(
    MultiheadAttention(head, size, hs, size; future=false, pdrop=pdrop),
    LayerNorm(size),
    MultiheadAttention(head, size, hs, size; future=true, pdrop=pdrop),
    LayerNorm(size),
    PwFFN(size, ps, act),
    LayerNorm(size),
    Dropout(pdrop),
)

function (td::MusicTransformerDecoder)(x::AbstractArray{T,N}, m, mask=nothing) where {T,N}
    dropout = td.drop

    # Self-Attention layer
    x_normed = td.mhn(x)
    a = td.mh(x_normed, x_normed, x_normed)
    a = dropout(a)
    res_a = x + a

    # Encoder-Decoder attention
    res_a_normed = td.imhn(res_a)
    ia = td.imh(res_a_normed, m, m, mask=mask)
    ia = dropout(ia)
    res_ia = res_a + ia

    # Feed-forward layer
    res_ia_normed = td.pwn(res_ia)
    pwffn = td.pw(res_ia_normed)
    pwffn = dropout(pwffn)
    res_pwffn = res_ia + pwffn
    res_pwffn
end

function Base.show(io::IO, td::MusicTransformerDecoder)
    hs = div(size(td.imh.iqproj.W)[1], td.imh.head)
    h, ps = size(td.pw.dout.W)

    print(io, "MusicTransformerDecoder(")
    print(io, "head=$(td.mh.head), ")
    print(io, "head_size=$(hs), ")
    print(io, "pwffn_size=$(ps), ")
    print(io, "size=$(h)")
    if Flux.istraining()
        print(io, ", dropout=$(td.drop.p))")
    else
        print(io, ")")
    end
end

function PositionEncoding(size::Int, max_len::Int = 2048)
    # Follows the tensor2tensor implementation - which is used by the Music Transformer
    num_timescales = size / 2
    positions = Float32.(collect(0.0:max_len-1))
    log_timescale_increment = log(1e4) / (num_timescales-1)
    inv_timescales = Float32.(exp.(collect(0.0:(num_timescales-1)) .* -log_timescale_increment))
    scaled_time = unsqueeze(positions, 2) * unsqueeze(inv_timescales, 1)
    timing_signals = hcat(sin.(scaled_time), cos.(scaled_time))
    PositionEmbedding(false, collect(timing_signals'))
end

struct MultiheadRelativeAttention{R<:AbstractArray, Q<:Dense, K<:Dense, V<:Dense, O<:Dense, DP<:Dropout} <: AbstractAttention
    head::Int
    future::Bool
    relative_embedding::R
    iqproj::Q
    ikproj::K
    ivproj::V
    oproj::O
    drop::DP
end

function Flux.functor(mh::MultiheadRelativeAttention)
    (mh.relative_embedding, mh.iqproj, mh.ikproj, mh.ivproj, mh.oproj), m -> MultiheadRelativeAttention(
                                                                                mh.head,
                                                                                mh.future,
                                                                                m...,
                                                                                mh.drop)
end

function MultiheadRelativeAttention(head::Int,
                                    is::Int,
                                    hs::Int,
                                    os::Int,
                                    max_relative_position::Int;
                                    future::Bool=true,
                                    share_relative_embedding::Bool=true,
                                    pdrop = 0.1)

    if share_relative_embedding
        relative_embedding = randn(Float32, hs, max_relative_position)
    else
        relative_embedding = randn(Float32, hs, max_relative_position, heads)
    end

    MultiheadRelativeAttention(head, future, relative_embedding,
                              Dense(is, hs*head), Dense(is, hs*head), Dense(is, hs*head), Dense(hs*head, os),
                              Dropout(pdrop))
end

function Base.show(io::IO, mh::MultiheadRelativeAttention)
    hs = div(size(mh.iqproj.W)[1], mh.head)
    is = size(mh.iqproj.W)[end]
    os = size(mh.oproj.W)[1]

    print(io, "MultiheadRelativeAttention(")
    print(io, "head=$(mh.head), ")
    print(io, "head_size=$(hs), ")
    print(io, "$(is)=>$(os)")

    if Flux.istraining()
        print(io, ", dropout=$(mh.drop.p))")
    else
        print(io, ")")
    end
end

function (mh::MultiheadRelativeAttention)(query::A1,
                                  key::A2,
                                  value::A3;
                                  mask=nothing) where {T,
                                                       A1 <: Abstract3DTensor{T},
                                                       A2 <: Abstract3DTensor{T},
                                                       A3 <: Abstract3DTensor{T}}
    qs = size(query)
    ks = size(key)
    vs = size(value)

      #size(ipq) == (h, q_seq_len, batch)
    ipq = @toNd mh.iqproj(query)
    ipk = @toNd mh.ikproj(key)
    ipv = @toNd mh.ivproj(value)

    h = size(ipq, 1)
    hs = div(h, mh.head)

    #size(ipq) == (hs, q_seq_len, head, batch)
    ipq = permutedims(reshape(ipq, hs, mh.head, qs[2], qs[3]), [1, 3, 2, 4])
    ipk = permutedims(reshape(ipk, hs, mh.head, ks[2], ks[3]), [1, 3, 2, 4])
    ipv = permutedims(reshape(ipv, hs, mh.head, vs[2], vs[3]), [1, 3, 2, 4])

    #size(ipq) == (hs, q_seq_len, head * batch)
    ipq = reshape(ipq, hs, qs[2], :)
    ipk = reshape(ipk, hs, ks[2], :)
    ipv = reshape(ipv, hs, vs[2], :)

    atten = relative_attention(ipq,ipk,ipv, mh.relative_embedding,
                               mask, mh.future, mh.drop)

    atten = permutedims(reshape(atten, hs, qs[2], mh.head, qs[3]), [1, 3, 2, 4]) #size(atten) == (hs, head, ql, b)
    atten = reshape(atten, h, qs[2], qs[3]) #size(atten) == (h, ql, b)

    out = @toNd mh.oproj(atten)
    out #size(out) == (h, q_seq_len, batch)
end

function (mh::MultiheadRelativeAttention)(query::A1,
                                          key::A2,
                                          value::A3;
                                          mask=nothing) where {T,
                                                               A1 <: AbstractMatrix{T},
                                                               A2 <: AbstractMatrix{T},
                                                               A3 <: AbstractMatrix{T}}

    # size(query) == (dims, seq_len)
    ipq = mh.iqproj(query)
    ipk = mh.ikproj(key)
    ipv = mh.ivproj(value)

    h = size(ipq)[1] #h == hs * head
    hs = div(h, mh.head)

    #size(hq) == (hs, seq_len, head)
    hq = permutedims(reshape(ipq, hs, mh.head, :), [1, 3, 2])
    hk = permutedims(reshape(ipk, hs, mh.head, :), [1, 3, 2])
    hv = permutedims(reshape(ipv, hs, mh.head, :), [1, 3, 2])

    atten = relative_attention(hq, hk, hv, mh.relative_embedding,
                               mask, mh.future, mh.drop)

    # size(atten) == (head*hs, seq_len)
    atten = reshape(permutedims(atten, [1, 3, 2]), h, :)

    mh.oproj(atten)
end

function relative_attention(query::A1,
                            key::A2,
                            value::A3,
                            relative_embedding::A4,
                            mask, future::Bool,
                            dropout) where {T,
                                            A1 <: Abstract3DTensor{T},
                                            A2 <: Abstract3DTensor{T},
                                            A3 <: Abstract3DTensor{T},
                                            A4 <: Union{Abstract3DTensor{T}, AbstractMatrix{T}}}

    #size(query) == (dims, {q,k}_seq_len, batch) == size(key) == size(value)
    #size(score) == (k_seq_len, q_seq_len, batch)
    dk = size(key, 1)
    score = batchedmul(key, query; transA = true)

    # if share_relative_embedding: size(rel_embed) == (dims, q_seq_len)
    # else:                        size(rel_embed) == (dims, q_seq_len, heads)
    seq_len = size(key, 2)
    rel_embed = get_relative_embedding(relative_embedding, seq_len)

    # size(rel_score) == (k_seq_len, q_seq_len, batch)
    rel_score = mul_relative_keys(query, rel_embed)
    rel_score = _relative_to_absolute_position(rel_score)

    # Add relative positional information to attention score
    score += rel_score

    score = score ./ convert(T, sqrt(dk))

    score = Transformers.Basic.apply_mask(score, mask, future)
    score = softmax(score; dims=1)
    dropout !== nothing && (score = dropout(score))
    batchedmul(value, score) #size(return) == (dims, q_seq_len, batch)
end

get_relative_embedding(r::AbstractMatrix{T}, seq_len::Int) where {T} = r[:, end-seq_len+1:end]
get_relative_embedding(r::Abstract3DTensor{T}, seq_len::Int) where {T} = r[:, end-seq_len+1:end, :]

function mul_relative_keys(x::Abstract3DTensor{T}, y::AbstractMatrix{T}) where {T}
    # Heads share relative embedding
    @tullio z[m, l, h] := x[d, l, h] * y[d, m]
end

function mul_relative_keys(x::Abstract3DTensor{T}, y::Abstract3DTensor{T}) where {T}
    # Heads don't share relative embedding
    @tullio z[m, l, h] := x[d, l, h] * y[d, m, h]
end

function _relative_to_absolute_position(x::Abstract3DTensor{T}) where {T}
    # size(x) == (seq_len, seq_len, heads)
    _, seq_len, heads = size(x)

    # Padding
    x = vcat(zeros(T, 1, seq_len, heads), x)

    # Reshape
    x = reshape(x, (seq_len, seq_len+1, :))
    x = permutedims(x, (2, 1, 3))

    # Slice
    x = x[2:end, :, :]

    x
end

struct TransformerRelative{MA<:MultiheadRelativeAttention, LA<:LayerNorm, P<:PwFFN, LP<:LayerNorm, DP<:Dropout} <: AbstractTransformer
    mh::MA
    mhn::LA
    pw::P
    pwn::LP
    drop::DP
end

@functor TransformerRelative

"""

"""
function TransformerRelative(size::Int, head::Int, ps::Int, max_relative_position::Int;
                            future::Bool = true, act = relu, pdrop = 0.1)

    rem(size, head) != 0 && error("size not divisible by head")
    TransformerRelative(size, head, div(size, head), ps, max_relative_position;
                        future=future, act=act, pdrop=pdrop)
end

function TransformerRelative(size::Int, head::Int, hs::Int, ps::Int, max_relative_position::Int;
                             future::Bool = true, act = relu, pdrop = 0.1)

    TransformerRelative(
        MultiheadRelativeAttention(head, size, hs, size, max_relative_position; future=future, pdrop=pdrop),
        LayerNorm(size),
        PwFFN(size, ps, act),
        LayerNorm(size),
        Dropout(pdrop),
    )
end

function (t::TransformerRelative)(x::A, mask=nothing) where {T, N, A<:AbstractArray{T, N}}
    dropout = t.drop

    # Attention layer
    a = t.mh(x, x, x; mask=mask)
    a = dropout(a)
    res_a = x + a
    res_a = t.mhn(x)

    # Feed-forward layer
    pwffn = t.pw(res_a)
    pwffn = dropout(pwffn)
    res_pwffn = res_a + pwffn
    res_pwffn = t.pwn(res_a)
    res_pwffn
end

function Base.show(io::IO, t::TransformerRelative)
    hs = div(size(t.mh.iqproj.W)[1], t.mh.head)
    h, ps = size(t.pw.dout.W)
    m = size(t.mh.relative_embedding, 2)

    print(io, "TransformerRelative(")
    print(io, "head=$(t.mh.head), ")
    print(io, "head_size=$(hs), ")
    print(io, "pwffn_size=$(ps), ")
    print(io, "size=$(h), ")
    print(io, "max_relative_position=$(m)")
    if Flux.istraining()
        print(io, ", dropout=$(t.drop.p))")
    else
        print(io, ")")
    end
end


text:
# This file is auto-generated by AWSMetadata.jl
using AWS
using AWS.AWSServices: cost_explorer
using AWS.Compat
using AWS.UUIDs

"""
    CreateAnomalyMonitor()

Creates a new cost anomaly detection monitor with the requested type and monitor specification.

# Required Parameters
- `AnomalyMonitor`:  The cost anomaly detection monitor object that you want to create.

"""
create_anomaly_monitor(AnomalyMonitor; aws_config::AWSConfig=global_aws_config()) = cost_explorer("CreateAnomalyMonitor", Dict{String, Any}("AnomalyMonitor"=>AnomalyMonitor); aws_config=aws_config)
create_anomaly_monitor(AnomalyMonitor, args::AbstractDict{String, <:Any}; aws_config::AWSConfig=global_aws_config()) = cost_explorer("CreateAnomalyMonitor", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("AnomalyMonitor"=>AnomalyMonitor), args)); aws_config=aws_config)

"""
    CreateAnomalySubscription()

Adds a subscription to a cost anomaly detection monitor. You can use each subscription to define subscribers with email or SNS notifications. Email subscribers can set a dollar threshold and a time frequency for receiving notifications.

# Required Parameters
- `AnomalySubscription`:  The cost anomaly subscription object that you want to create.

"""
create_anomaly_subscription(AnomalySubscription; aws_config::AWSConfig=global_aws_config()) = cost_explorer("CreateAnomalySubscription", Dict{String, Any}("AnomalySubscription"=>AnomalySubscription); aws_config=aws_config)
create_anomaly_subscription(AnomalySubscription, args::AbstractDict{String, <:Any}; aws_config::AWSConfig=global_aws_config()) = cost_explorer("CreateAnomalySubscription", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("AnomalySubscription"=>AnomalySubscription), args)); aws_config=aws_config)

"""
    CreateCostCategoryDefinition()

Creates a new Cost Category with the requested name and rules.

# Required Parameters
- `Name`:
- `RuleVersion`:
- `Rules`: The Cost Category rules used to categorize costs. For more information, see CostCategoryRule.

"""
create_cost_category_definition(Name, RuleVersion, Rules; aws_config::AWSConfig=global_aws_config()) = cost_explorer("CreateCostCategoryDefinition", Dict{String, Any}("Name"=>Name, "RuleVersion"=>RuleVersion, "Rules"=>Rules); aws_config=aws_config)
create_cost_category_definition(Name, RuleVersion, Rules, args::AbstractDict{String, <:Any}; aws_config::AWSConfig=global_aws_config()) = cost_explorer("CreateCostCategoryDefinition", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("Name"=>Name, "RuleVersion"=>RuleVersion, "Rules"=>Rules), args)); aws_config=aws_config)

"""
    DeleteAnomalyMonitor()

Deletes a cost anomaly monitor.

# Required Parameters
- `MonitorArn`:  The unique identifier of the cost anomaly monitor that you want to delete.

"""
delete_anomaly_monitor(MonitorArn; aws_config::AWSConfig=global_aws_config()) = cost_explorer("DeleteAnomalyMonitor", Dict{String, Any}("MonitorArn"=>MonitorArn); aws_config=aws_config)
delete_anomaly_monitor(MonitorArn, args::AbstractDict{String, <:Any}; aws_config::AWSConfig=global_aws_config()) = cost_explorer("DeleteAnomalyMonitor", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("MonitorArn"=>MonitorArn), args)); aws_config=aws_config)

"""
    DeleteAnomalySubscription()

Deletes a cost anomaly subscription.

# Required Parameters
- `SubscriptionArn`:  The unique identifier of the cost anomaly subscription that you want to delete.

"""
delete_anomaly_subscription(SubscriptionArn; aws_config::AWSConfig=global_aws_config()) = cost_explorer("DeleteAnomalySubscription", Dict{String, Any}("SubscriptionArn"=>SubscriptionArn); aws_config=aws_config)
delete_anomaly_subscription(SubscriptionArn, args::AbstractDict{String, <:Any}; aws_config::AWSConfig=global_aws_config()) = cost_explorer("DeleteAnomalySubscription", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("SubscriptionArn"=>SubscriptionArn), args)); aws_config=aws_config)

"""
    DeleteCostCategoryDefinition()

Deletes a Cost Category. Expenses from this month going forward will no longer be categorized with this Cost Category.

# Required Parameters
- `CostCategoryArn`:  The unique identifier for your Cost Category.

"""
delete_cost_category_definition(CostCategoryArn; aws_config::AWSConfig=global_aws_config()) = cost_explorer("DeleteCostCategoryDefinition", Dict{String, Any}("CostCategoryArn"=>CostCategoryArn); aws_config=aws_config)
delete_cost_category_definition(CostCategoryArn, args::AbstractDict{String, <:Any}; aws_config::AWSConfig=global_aws_config()) = cost_explorer("DeleteCostCategoryDefinition", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("CostCategoryArn"=>CostCategoryArn), args)); aws_config=aws_config)

"""
    DescribeCostCategoryDefinition()

Returns the name, ARN, rules, definition, and effective dates of a Cost Category that's defined in the account. You have the option to use EffectiveOn to return a Cost Category that is active on a specific date. If there is no EffectiveOn specified, you’ll see a Cost Category that is effective on the current date. If Cost Category is still effective, EffectiveEnd is omitted in the response.

# Required Parameters
- `CostCategoryArn`:  The unique identifier for your Cost Category.

# Optional Parameters
- `EffectiveOn`:  The date when the Cost Category was effective.
"""
describe_cost_category_definition(CostCategoryArn; aws_config::AWSConfig=global_aws_config()) = cost_explorer("DescribeCostCategoryDefinition", Dict{String, Any}("CostCategoryArn"=>CostCategoryArn); aws_config=aws_config)
describe_cost_category_definition(CostCategoryArn, args::AbstractDict{String, <:Any}; aws_config::AWSConfig=global_aws_config()) = cost_explorer("DescribeCostCategoryDefinition", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("CostCategoryArn"=>CostCategoryArn), args)); aws_config=aws_config)

"""
    GetAnomalies()

Retrieves all of the cost anomalies detected on your account, during the time period specified by the DateInterval object.

# Required Parameters
- `DateInterval`: Assigns the start and end dates for retrieving cost anomalies. The returned anomaly object will have an AnomalyEndDate in the specified time range.

# Optional Parameters
- `Feedback`: Filters anomaly results by the feedback field on the anomaly object.
- `MaxResults`:  The number of entries a paginated response contains.
- `MonitorArn`: Retrieves all of the cost anomalies detected for a specific cost anomaly monitor Amazon Resource Name (ARN).
- `NextPageToken`:  The token to retrieve the next set of results. AWS provides the token when the response from a previous call has more results than the maximum page size.
- `TotalImpact`: Filters anomaly results by the total impact field on the anomaly object. For example, you can filter anomalies GREATER_THAN 200.00 to retrieve anomalies, with an estimated dollar impact greater than 200.
"""
get_anomalies(DateInterval; aws_config::AWSConfig=global_aws_config()) = cost_explorer("GetAnomalies", Dict{String, Any}("DateInterval"=>DateInterval); aws_config=aws_config)
get_anomalies(DateInterval, args::AbstractDict{String, <:Any}; aws_config::AWSConfig=global_aws_config()) = cost_explorer("GetAnomalies", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("DateInterval"=>DateInterval), args)); aws_config=aws_config)

"""
    GetAnomalyMonitors()

Retrieves the cost anomaly monitor definitions for your account. You can filter using a list of cost anomaly monitor Amazon Resource Names (ARNs).

# Optional Parameters
- `MaxResults`:  The number of entries a paginated response contains.
- `MonitorArnList`:  A list of cost anomaly monitor ARNs.
- `NextPageToken`:  The token to retrieve the next set of results. AWS provides the token when the response from a previous call has more results than the maximum page size.
"""
get_anomaly_monitors(; aws_config::AWSConfig=global_aws_config()) = cost_explorer("GetAnomalyMonitors"; aws_config=aws_config)
get_anomaly_monitors(args::AbstractDict{String, <:Any}; aws_config::AWSConfig=global_aws_config()) = cost_explorer("GetAnomalyMonitors", args; aws_config=aws_config)

"""
    GetAnomalySubscriptions()

Retrieves the cost anomaly subscription objects for your account. You can filter using a list of cost anomaly monitor Amazon Resource Names (ARNs).

# Optional Parameters
- `MaxResults`:  The number of entries a paginated response contains.
- `MonitorArn`:  Cost anomaly monitor ARNs.
- `NextPageToken`:  The token to retrieve the next set of results. AWS provides the token when the response from a previous call has more results than the maximum page size.
- `SubscriptionArnList`:  A list of cost anomaly subscription ARNs.
"""
get_anomaly_subscriptions(; aws_config::AWSConfig=global_aws_config()) = cost_explorer("GetAnomalySubscriptions"; aws_config=aws_config)
get_anomaly_subscriptions(args::AbstractDict{String, <:Any}; aws_config::AWSConfig=global_aws_config()) = cost_explorer("GetAnomalySubscriptions", args; aws_config=aws_config)

"""
    GetCostAndUsage()

Retrieves cost and usage metrics for your account. You can specify which cost and usage-related metric, such as BlendedCosts or UsageQuantity, that you want the request to return. You can also filter and group your data by various dimensions, such as SERVICE or AZ, in a specific time range. For a complete list of valid dimensions, see the GetDimensionValues operation. Management account in an organization in AWS Organizations have access to all member accounts. For information about filter limitations, see Quotas and restrictions in the Billing and Cost Management User Guide.

# Required Parameters
- `Metrics`: Which metrics are returned in the query. For more information about blended and unblended rates, see Why does the \"blended\" annotation appear on some line items in my bill?.  Valid values are AmortizedCost, BlendedCost, NetAmortizedCost, NetUnblendedCost, NormalizedUsageAmount, UnblendedCost, and UsageQuantity.   If you return the UsageQuantity metric, the service aggregates all usage numbers without taking into account the units. For example, if you aggregate usageQuantity across all of Amazon EC2, the results aren't meaningful because Amazon EC2 compute hours and data transfer are measured in different units (for example, hours vs. GB). To get more meaningful UsageQuantity metrics, filter by UsageType or UsageTypeGroups.    Metrics is required for GetCostAndUsage requests.
- `TimePeriod`: Sets the start and end dates for retrieving AWS costs. The start date is inclusive, but the end date is exclusive. For example, if start is 2017-01-01 and end is 2017-05-01, then the cost and usage data is retrieved from 2017-01-01 up to and including 2017-04-30 but not including 2017-05-01.

# Optional Parameters
- `Filter`: Filters AWS costs by different dimensions. For example, you can specify SERVICE and LINKED_ACCOUNT and get the costs that are associated with that account's usage of that service. You can nest Expression objects to define any combination of dimension filters. For more information, see Expression.
- `Granularity`: Sets the AWS cost granularity to MONTHLY or DAILY, or HOURLY. If Granularity isn't set, the response object doesn't include the Granularity, either MONTHLY or DAILY, or HOURLY.
- `GroupBy`: You can group AWS costs using up to two different groups, either dimensions, tag keys, cost categories, or any two group by types. When you group by tag key, you get all tag values, including empty strings. Valid values are AZ, INSTANCE_TYPE, LEGAL_ENTITY_NAME, LINKED_ACCOUNT, OPERATION, PLATFORM, PURCHASE_TYPE, SERVICE, TAGS, TENANCY, RECORD_TYPE, and USAGE_TYPE.
- `NextPageToken`: The token to retrieve the next set of results. AWS provides the token when the response from a previous call has more results than the maximum page size.
"""
get_cost_and_usage(Metrics, TimePeriod; aws_config::AWSConfig=global_aws_config()) = cost_explorer("GetCostAndUsage", Dict{String, Any}("Metrics"=>Metrics, "TimePeriod"=>TimePeriod); aws_config=aws_config)
get_cost_and_usage(Metrics, TimePeriod, args::AbstractDict{String, <:Any}; aws_config::AWSConfig=global_aws_config()) = cost_explorer("GetCostAndUsage", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("Metrics"=>Metrics, "TimePeriod"=>TimePeriod), args)); aws_config=aws_config)

"""
    GetCostAndUsageWithResources()

Retrieves cost and usage metrics with resources for your account. You can specify which cost and usage-related metric, such as BlendedCosts or UsageQuantity, that you want the request to return. You can also filter and group your data by various dimensions, such as SERVICE or AZ, in a specific time range. For a complete list of valid dimensions, see the GetDimensionValues operation. Management account in an organization in AWS Organizations have access to all member accounts. This API is currently available for the Amazon Elastic Compute Cloud – Compute service only.  This is an opt-in only feature. You can enable this feature from the Cost Explorer Settings page. For information on how to access the Settings page, see Controlling Access for Cost Explorer in the AWS Billing and Cost Management User Guide.

# Required Parameters
- `Filter`: Filters Amazon Web Services costs by different dimensions. For example, you can specify SERVICE and LINKED_ACCOUNT and get the costs that are associated with that account's usage of that service. You can nest Expression objects to define any combination of dimension filters. For more information, see Expression.  The GetCostAndUsageWithResources operation requires that you either group by or filter by a ResourceId. It requires the Expression \"SERVICE = Amazon Elastic Compute Cloud - Compute\" in the filter.
- `TimePeriod`: Sets the start and end dates for retrieving Amazon Web Services costs. The range must be within the last 14 days (the start date cannot be earlier than 14 days ago). The start date is inclusive, but the end date is exclusive. For example, if start is 2017-01-01 and end is 2017-05-01, then the cost and usage data is retrieved from 2017-01-01 up to and including 2017-04-30 but not including 2017-05-01.

# Optional Parameters
- `Granularity`: Sets the AWS cost granularity to MONTHLY, DAILY, or HOURLY. If Granularity isn't set, the response object doesn't include the Granularity, MONTHLY, DAILY, or HOURLY.
- `GroupBy`: You can group Amazon Web Services costs using up to two different groups: DIMENSION, TAG, COST_CATEGORY.
- `Metrics`: Which metrics are returned in the query. For more information about blended and unblended rates, see Why does the \"blended\" annotation appear on some line items in my bill?.  Valid values are AmortizedCost, BlendedCost, NetAmortizedCost, NetUnblendedCost, NormalizedUsageAmount, UnblendedCost, and UsageQuantity.   If you return the UsageQuantity metric, the service aggregates all usage numbers without taking the units into account. For example, if you aggregate usageQuantity across all of Amazon EC2, the results aren't meaningful because Amazon EC2 compute hours and data transfer are measured in different units (for example, hours vs. GB). To get more meaningful UsageQuantity metrics, filter by UsageType or UsageTypeGroups.    Metrics is required for GetCostAndUsageWithResources requests.
- `NextPageToken`: The token to retrieve the next set of results. AWS provides the token when the response from a previous call has more results than the maximum page size.
"""
get_cost_and_usage_with_resources(Filter, TimePeriod; aws_config::AWSConfig=global_aws_config()) = cost_explorer("GetCostAndUsageWithResources", Dict{String, Any}("Filter"=>Filter, "TimePeriod"=>TimePeriod); aws_config=aws_config)
get_cost_and_usage_with_resources(Filter, TimePeriod, args::AbstractDict{String, <:Any}; aws_config::AWSConfig=global_aws_config()) = cost_explorer("GetCostAndUsageWithResources", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("Filter"=>Filter, "TimePeriod"=>TimePeriod), args)); aws_config=aws_config)

"""
    GetCostForecast()

Retrieves a forecast for how much Amazon Web Services predicts that you will spend over the forecast time period that you select, based on your past costs.

# Required Parameters
- `Granularity`: How granular you want the forecast to be. You can get 3 months of DAILY forecasts or 12 months of MONTHLY forecasts. The GetCostForecast operation supports only DAILY and MONTHLY granularities.
- `Metric`: Which metric Cost Explorer uses to create your forecast. For more information about blended and unblended rates, see Why does the \"blended\" annotation appear on some line items in my bill?.  Valid values for a GetCostForecast call are the following:   AMORTIZED_COST   BLENDED_COST   NET_AMORTIZED_COST   NET_UNBLENDED_COST   UNBLENDED_COST
- `TimePeriod`: The period of time that you want the forecast to cover. The start date must be equal to or no later than the current date to avoid a validation error.

# Optional Parameters
- `Filter`: The filters that you want to use to filter your forecast. Cost Explorer API supports all of the Cost Explorer filters.
- `PredictionIntervalLevel`: Cost Explorer always returns the mean forecast as a single point. You can request a prediction interval around the mean by specifying a confidence level. The higher the confidence level, the more confident Cost Explorer is about the actual value falling in the prediction interval. Higher confidence levels result in wider prediction intervals.
"""
get_cost_forecast(Granularity, Metric, TimePeriod; aws_config::AWSConfig=global_aws_config()) = cost_explorer("GetCostForecast", Dict{String, Any}("Granularity"=>Granularity, "Metric"=>Metric, "TimePeriod"=>TimePeriod); aws_config=aws_config)
get_cost_forecast(Granularity, Metric, TimePeriod, args::AbstractDict{String, <:Any}; aws_config::AWSConfig=global_aws_config()) = cost_explorer("GetCostForecast", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("Granularity"=>Granularity, "Metric"=>Metric, "TimePeriod"=>TimePeriod), args)); aws_config=aws_config)

"""
    GetDimensionValues()

Retrieves all available filter values for a specified filter over a period of time. You can search the dimension values for an arbitrary string.

# Required Parameters
- `Dimension`: The name of the dimension. Each Dimension is available for a different Context. For more information, see Context.
- `TimePeriod`: The start and end dates for retrieving the dimension values. The start date is inclusive, but the end date is exclusive. For example, if start is 2017-01-01 and end is 2017-05-01, then the cost and usage data is retrieved from 2017-01-01 up to and including 2017-04-30 but not including 2017-05-01.

# Optional Parameters
- `Context`: The context for the call to GetDimensionValues. This can be RESERVATIONS or COST_AND_USAGE. The default value is COST_AND_USAGE. If the context is set to RESERVATIONS, the resulting dimension values can be used in the GetReservationUtilization operation. If the context is set to COST_AND_USAGE, the resulting dimension values can be used in the GetCostAndUsage operation. If you set the context to COST_AND_USAGE, you can use the following dimensions for searching:   AZ - The Availability Zone. An example is us-east-1a.   DATABASE_ENGINE - The Amazon Relational Database Service database. Examples are Aurora or MySQL.   INSTANCE_TYPE - The type of Amazon EC2 instance. An example is m4.xlarge.   LEGAL_ENTITY_NAME - The name of the organization that sells you AWS services, such as Amazon Web Services.   LINKED_ACCOUNT - The description in the attribute map that includes the full name of the member account. The value field contains the AWS ID of the member account.   OPERATING_SYSTEM - The operating system. Examples are Windows or Linux.   OPERATION - The action performed. Examples include RunInstance and CreateBucket.   PLATFORM - The Amazon EC2 operating system. Examples are Windows or Linux.   PURCHASE_TYPE - The reservation type of the purchase to which this usage is related. Examples include On-Demand Instances and Standard Reserved Instances.   SERVICE - The AWS service such as Amazon DynamoDB.   USAGE_TYPE - The type of usage. An example is DataTransfer-In-Bytes. The response for the GetDimensionValues operation includes a unit attribute. Examples include GB and Hrs.   USAGE_TYPE_GROUP - The grouping of common usage types. An example is Amazon EC2: CloudWatch – Alarms. The response for this operation includes a unit attribute.   REGION - The AWS Region.   RECORD_TYPE - The different types of charges such as RI fees, usage costs, tax refunds, and credits.   RESOURCE_ID - The unique identifier of the resource. ResourceId is an opt-in feature only available for last 14 days for EC2-Compute Service.   If you set the context to RESERVATIONS, you can use the following dimensions for searching:   AZ - The Availability Zone. An example is us-east-1a.   CACHE_ENGINE - The Amazon ElastiCache operating system. Examples are Windows or Linux.   DEPLOYMENT_OPTION - The scope of Amazon Relational Database Service deployments. Valid values are SingleAZ and MultiAZ.   INSTANCE_TYPE - The type of Amazon EC2 instance. An example is m4.xlarge.   LINKED_ACCOUNT - The description in the attribute map that includes the full name of the member account. The value field contains the AWS ID of the member account.   PLATFORM - The Amazon EC2 operating system. Examples are Windows or Linux.   REGION - The AWS Region.   SCOPE (Utilization only) - The scope of a Reserved Instance (RI). Values are regional or a single Availability Zone.   TAG (Coverage only) - The tags that are associated with a Reserved Instance (RI).   TENANCY - The tenancy of a resource. Examples are shared or dedicated.   If you set the context to SAVINGS_PLANS, you can use the following dimensions for searching:   SAVINGS_PLANS_TYPE - Type of Savings Plans (EC2 Instance or Compute)   PAYMENT_OPTION - Payment option for the given Savings Plans (for example, All Upfront)   REGION - The AWS Region.   INSTANCE_TYPE_FAMILY - The family of instances (For example, m5)   LINKED_ACCOUNT - The description in the attribute map that includes the full name of the member account. The value field contains the AWS ID of the member account.   SAVINGS_PLAN_ARN - The unique identifier for your Savings Plan
- `NextPageToken`: The token to retrieve the next set of results. AWS provides the token when the response from a previous call has more results than the maximum page size.
- `SearchString`: The value that you want to search the filter values for.
"""
get_dimension_values(Dimension, TimePeriod; aws_config::AWSConfig=global_aws_config()) = cost_explorer("GetDimensionValues", Dict{String, Any}("Dimension"=>Dimension, "TimePeriod"=>TimePeriod); aws_config=aws_config)
get_dimension_values(Dimension, TimePeriod, args::AbstractDict{String, <:Any}; aws_config::AWSConfig=global_aws_config()) = cost_explorer("GetDimensionValues", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("Dimension"=>Dimension, "TimePeriod"=>TimePeriod), args)); aws_config=aws_config)

"""
    GetReservationCoverage()

Retrieves the reservation coverage for your account. This enables you to see how much of your Amazon Elastic Compute Cloud, Amazon ElastiCache, Amazon Relational Database Service, or Amazon Redshift usage is covered by a reservation. An organization's management account can see the coverage of the associated member accounts. This supports dimensions, Cost Categories, and nested expressions. For any time period, you can filter data about reservation usage by the following dimensions:   AZ   CACHE_ENGINE   DATABASE_ENGINE   DEPLOYMENT_OPTION   INSTANCE_TYPE   LINKED_ACCOUNT   OPERATING_SYSTEM   PLATFORM   REGION   SERVICE   TAG   TENANCY   To determine valid values for a dimension, use the GetDimensionValues operation.

# Required Parameters
- `TimePeriod`: The start and end dates of the period that you want to retrieve data about reservation coverage for. You can retrieve data for a maximum of 13 months: the last 12 months and the current month. The start date is inclusive, but the end date is exclusive. For example, if start is 2017-01-01 and end is 2017-05-01, then the cost and usage data is retrieved from 2017-01-01 up to and including 2017-04-30 but not including 2017-05-01.

# Optional Parameters
- `Filter`: Filters utilization data by dimensions. You can filter by the following dimensions:   AZ   CACHE_ENGINE   DATABASE_ENGINE   DEPLOYMENT_OPTION   INSTANCE_TYPE   LINKED_ACCOUNT   OPERATING_SYSTEM   PLATFORM   REGION   SERVICE   TAG   TENANCY    GetReservationCoverage uses the same Expression object as the other operations, but only AND is supported among each dimension. You can nest only one level deep. If there are multiple values for a dimension, they are OR'd together. If you don't provide a SERVICE filter, Cost Explorer defaults to EC2. Cost category is also supported.
- `Granularity`: The granularity of the AWS cost data for the reservation. Valid values are MONTHLY and DAILY. If GroupBy is set, Granularity can't be set. If Granularity isn't set, the response object doesn't include Granularity, either MONTHLY or DAILY. The GetReservationCoverage operation supports only DAILY and MONTHLY granularities.
- `GroupBy`: You can group the data by the following attributes:   AZ   CACHE_ENGINE   DATABASE_ENGINE   DEPLOYMENT_OPTION   INSTANCE_TYPE   LINKED_ACCOUNT   OPERATING_SYSTEM   PLATFORM   REGION   TENANCY
- `Metrics`: The measurement that you want your reservation coverage reported in. Valid values are Hour, Unit, and Cost. You can use multiple values in a request.
- `NextPageToken`: The token to retrieve the next set of results. AWS provides the token when the response from a previous call has more results than the maximum page size.
"""
get_reservation_coverage(TimePeriod; aws_config::AWSConfig=global_aws_config()) = cost_explorer("GetReservationCoverage", Dict{String, Any}("TimePeriod"=>TimePeriod); aws_config=aws_config)
get_reservation_coverage(TimePeriod, args::AbstractDict{String, <:Any}; aws_config::AWSConfig=global_aws_config()) = cost_explorer("GetReservationCoverage", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("TimePeriod"=>TimePeriod), args)); aws_config=aws_config)

"""
    GetReservationPurchaseRecommendation()

Gets recommendations for which reservations to purchase. These recommendations could help you reduce your costs. Reservations provide a discounted hourly rate (up to 75%) compared to On-Demand pricing. AWS generates your recommendations by identifying your On-Demand usage during a specific time period and collecting your usage into categories that are eligible for a reservation. After AWS has these categories, it simulates every combination of reservations in each category of usage to identify the best number of each type of RI to purchase to maximize your estimated savings.  For example, AWS automatically aggregates your Amazon EC2 Linux, shared tenancy, and c4 family usage in the US West (Oregon) Region and recommends that you buy size-flexible regional reservations to apply to the c4 family usage. AWS recommends the smallest size instance in an instance family. This makes it easier to purchase a size-flexible RI. AWS also shows the equal number of normalized units so that you can purchase any instance size that you want. For this example, your RI recommendation would be for c4.large because that is the smallest size instance in the c4 instance family.

# Required Parameters
- `Service`: The specific service that you want recommendations for.

# Optional Parameters
- `AccountId`: The account ID that is associated with the recommendation.
- `AccountScope`: The account scope that you want your recommendations for. Amazon Web Services calculates recommendations including the management account and member accounts if the value is set to PAYER. If the value is LINKED, recommendations are calculated for individual member accounts only.
- `LookbackPeriodInDays`: The number of previous days that you want AWS to consider when it calculates your recommendations.
- `NextPageToken`: The pagination token that indicates the next set of results that you want to retrieve.
- `PageSize`: The number of recommendations that you want returned in a single response object.
- `PaymentOption`: The reservation purchase option that you want recommendations for.
- `ServiceSpecification`: The hardware specifications for the service instances that you want recommendations for, such as standard or convertible Amazon EC2 instances.
- `TermInYears`: The reservation term that you want recommendations for.
"""
get_reservation_purchase_recommendation(Service; aws_config::AWSConfig=global_aws_config()) = cost_explorer("GetReservationPurchaseRecommendation", Dict{String, Any}("Service"=>Service); aws_config=aws_config)
get_reservation_purchase_recommendation(Service, args::AbstractDict{String, <:Any}; aws_config::AWSConfig=global_aws_config()) = cost_explorer("GetReservationPurchaseRecommendation", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("Service"=>Service), args)); aws_config=aws_config)

"""
    GetReservationUtilization()

Retrieves the reservation utilization for your account. Management account in an organization have access to member accounts. You can filter data by dimensions in a time period. You can use GetDimensionValues to determine the possible dimension values. Currently, you can group only by SUBSCRIPTION_ID.

# Required Parameters
- `TimePeriod`: Sets the start and end dates for retrieving RI utilization. The start date is inclusive, but the end date is exclusive. For example, if start is 2017-01-01 and end is 2017-05-01, then the cost and usage data is retrieved from 2017-01-01 up to and including 2017-04-30 but not including 2017-05-01.

# Optional Parameters
- `Filter`: Filters utilization data by dimensions. You can filter by the following dimensions:   AZ   CACHE_ENGINE   DEPLOYMENT_OPTION   INSTANCE_TYPE   LINKED_ACCOUNT   OPERATING_SYSTEM   PLATFORM   REGION   SERVICE   SCOPE   TENANCY    GetReservationUtilization uses the same Expression object as the other operations, but only AND is supported among each dimension, and nesting is supported up to only one level deep. If there are multiple values for a dimension, they are OR'd together.
- `Granularity`: If GroupBy is set, Granularity can't be set. If Granularity isn't set, the response object doesn't include Granularity, either MONTHLY or DAILY. If both GroupBy and Granularity aren't set, GetReservationUtilization defaults to DAILY. The GetReservationUtilization operation supports only DAILY and MONTHLY granularities.
- `GroupBy`: Groups only by SUBSCRIPTION_ID. Metadata is included.
- `NextPageToken`: The token to retrieve the next set of results. AWS provides the token when the response from a previous call has more results than the maximum page size.
"""
get_reservation_utilization(TimePeriod; aws_config::AWSConfig=global_aws_config()) = cost_explorer("GetReservationUtilization", Dict{String, Any}("TimePeriod"=>TimePeriod); aws_config=aws_config)
get_reservation_utilization(TimePeriod, args::AbstractDict{String, <:Any}; aws_config::AWSConfig=global_aws_config()) = cost_explorer("GetReservationUtilization", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("TimePeriod"=>TimePeriod), args)); aws_config=aws_config)

"""
    GetRightsizingRecommendation()

Creates recommendations that help you save cost by identifying idle and underutilized Amazon EC2 instances. Recommendations are generated to either downsize or terminate instances, along with providing savings detail and metrics. For details on calculation and function, see Optimizing Your Cost with Rightsizing Recommendations in the AWS Billing and Cost Management User Guide.

# Required Parameters
- `Service`: The specific service that you want recommendations for. The only valid value for GetRightsizingRecommendation is \"AmazonEC2\".

# Optional Parameters
- `Configuration`:  Enables you to customize recommendations across two attributes. You can choose to view recommendations for instances within the same instance families or across different instance families. You can also choose to view your estimated savings associated with recommendations with consideration of existing Savings Plans or RI benefits, or neither.
- `Filter`:
- `NextPageToken`: The pagination token that indicates the next set of results that you want to retrieve.
- `PageSize`: The number of recommendations that you want returned in a single response object.
"""
get_rightsizing_recommendation(Service; aws_config::AWSConfig=global_aws_config()) = cost_explorer("GetRightsizingRecommendation", Dict{String, Any}("Service"=>Service); aws_config=aws_config)
get_rightsizing_recommendation(Service, args::AbstractDict{String, <:Any}; aws_config::AWSConfig=global_aws_config()) = cost_explorer("GetRightsizingRecommendation", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("Service"=>Service), args)); aws_config=aws_config)

"""
    GetSavingsPlansCoverage()

Retrieves the Savings Plans covered for your account. This enables you to see how much of your cost is covered by a Savings Plan. An organization’s management account can see the coverage of the associated member accounts. This supports dimensions, Cost Categories, and nested expressions. For any time period, you can filter data for Savings Plans usage with the following dimensions:    LINKED_ACCOUNT     REGION     SERVICE     INSTANCE_FAMILY    To determine valid values for a dimension, use the GetDimensionValues operation.

# Required Parameters
- `TimePeriod`: The time period that you want the usage and costs for. The Start date must be within 13 months. The End date must be after the Start date, and before the current date. Future dates can't be used as an End date.

# Optional Parameters
- `Filter`: Filters Savings Plans coverage data by dimensions. You can filter data for Savings Plans usage with the following dimensions:    LINKED_ACCOUNT     REGION     SERVICE     INSTANCE_FAMILY     GetSavingsPlansCoverage uses the same Expression object as the other operations, but only AND is supported among each dimension. If there are multiple values for a dimension, they are OR'd together. Cost category is also supported.
- `Granularity`: The granularity of the Amazon Web Services cost data for your Savings Plans. Granularity can't be set if GroupBy is set. The GetSavingsPlansCoverage operation supports only DAILY and MONTHLY granularities.
- `GroupBy`: You can group the data using the attributes INSTANCE_FAMILY, REGION, or SERVICE.
- `MaxResults`: The number of items to be returned in a response. The default is 20, with a minimum value of 1.
- `Metrics`: The measurement that you want your Savings Plans coverage reported in. The only valid value is SpendCoveredBySavingsPlans.
- `NextToken`: The token to retrieve the next set of results. Amazon Web Services provides the token when the response from a previous call has more results than the maximum page size.
"""
get_savings_plans_coverage(TimePeriod; aws_config::AWSConfig=global_aws_config()) = cost_explorer("GetSavingsPlansCoverage", Dict{String, Any}("TimePeriod"=>TimePeriod); aws_config=aws_config)
get_savings_plans_coverage(TimePeriod, args::AbstractDict{String, <:Any}; aws_config::AWSConfig=global_aws_config()) = cost_explorer("GetSavingsPlansCoverage", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("TimePeriod"=>TimePeriod), args)); aws_config=aws_config)

"""
    GetSavingsPlansPurchaseRecommendation()

Retrieves your request parameters, Savings Plan Recommendations Summary and Details.

# Required Parameters
- `LookbackPeriodInDays`: The lookback period used to generate the recommendation.
- `PaymentOption`: The payment option used to generate these recommendations.
- `SavingsPlansType`: The Savings Plans recommendation type requested.
- `TermInYears`: The savings plan recommendation term used to generate these recommendations.

# Optional Parameters
- `AccountScope`: The account scope that you want your recommendations for. Amazon Web Services calculates recommendations including the management account and member accounts if the value is set to PAYER. If the value is LINKED, recommendations are calculated for individual member accounts only.
- `Filter`: You can filter your recommendations by Account ID with the LINKED_ACCOUNT dimension. To filter your recommendations by Account ID, specify Key as LINKED_ACCOUNT and Value as the comma-separated Acount ID(s) for which you want to see Savings Plans purchase recommendations. For GetSavingsPlansPurchaseRecommendation, the Filter does not include CostCategories or Tags. It only includes Dimensions. With Dimensions, Key must be LINKED_ACCOUNT and Value can be a single Account ID or multiple comma-separated Account IDs for which you want to see Savings Plans Purchase Recommendations. AND and OR operators are not supported.
- `NextPageToken`: The token to retrieve the next set of results. Amazon Web Services provides the token when the response from a previous call has more results than the maximum page size.
- `PageSize`: The number of recommendations that you want returned in a single response object.
"""
get_savings_plans_purchase_recommendation(LookbackPeriodInDays, PaymentOption, SavingsPlansType, TermInYears; aws_config::AWSConfig=global_aws_config()) = cost_explorer("GetSavingsPlansPurchaseRecommendation", Dict{String, Any}("LookbackPeriodInDays"=>LookbackPeriodInDays, "PaymentOption"=>PaymentOption, "SavingsPlansType"=>SavingsPlansType, "TermInYears"=>TermInYears); aws_config=aws_config)
get_savings_plans_purchase_recommendation(LookbackPeriodInDays, PaymentOption, SavingsPlansType, TermInYears, args::AbstractDict{String, <:Any}; aws_config::AWSConfig=global_aws_config()) = cost_explorer("GetSavingsPlansPurchaseRecommendation", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("LookbackPeriodInDays"=>LookbackPeriodInDays, "PaymentOption"=>PaymentOption, "SavingsPlansType"=>SavingsPlansType, "TermInYears"=>TermInYears), args)); aws_config=aws_config)

"""
    GetSavingsPlansUtilization()

Retrieves the Savings Plans utilization for your account across date ranges with daily or monthly granularity. Management account in an organization have access to member accounts. You can use GetDimensionValues in SAVINGS_PLANS to determine the possible dimension values.  You cannot group by any dimension values for GetSavingsPlansUtilization.

# Required Parameters
- `TimePeriod`: The time period that you want the usage and costs for. The Start date must be within 13 months. The End date must be after the Start date, and before the current date. Future dates can't be used as an End date.

# Optional Parameters
- `Filter`: Filters Savings Plans utilization coverage data for active Savings Plans dimensions. You can filter data with the following dimensions:    LINKED_ACCOUNT     SAVINGS_PLAN_ARN     SAVINGS_PLANS_TYPE     REGION     PAYMENT_OPTION     INSTANCE_TYPE_FAMILY     GetSavingsPlansUtilization uses the same Expression object as the other operations, but only AND is supported among each dimension.
- `Granularity`: The granularity of the Amazon Web Services utillization data for your Savings Plans. The GetSavingsPlansUtilization operation supports only DAILY and MONTHLY granularities.
"""
get_savings_plans_utilization(TimePeriod; aws_config::AWSConfig=global_aws_config()) = cost_explorer("GetSavingsPlansUtilization", Dict{String, Any}("TimePeriod"=>TimePeriod); aws_config=aws_config)
get_savings_plans_utilization(TimePeriod, args::AbstractDict{String, <:Any}; aws_config::AWSConfig=global_aws_config()) = cost_explorer("GetSavingsPlansUtilization", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("TimePeriod"=>TimePeriod), args)); aws_config=aws_config)

"""
    GetSavingsPlansUtilizationDetails()

Retrieves attribute data along with aggregate utilization and savings data for a given time period. This doesn't support granular or grouped data (daily/monthly) in response. You can't retrieve data by dates in a single response similar to GetSavingsPlanUtilization, but you have the option to make multiple calls to GetSavingsPlanUtilizationDetails by providing individual dates. You can use GetDimensionValues in SAVINGS_PLANS to determine the possible dimension values.   GetSavingsPlanUtilizationDetails internally groups data by SavingsPlansArn.

# Required Parameters
- `TimePeriod`: The time period that you want the usage and costs for. The Start date must be within 13 months. The End date must be after the Start date, and before the current date. Future dates can't be used as an End date.

# Optional Parameters
- `Filter`: Filters Savings Plans utilization coverage data for active Savings Plans dimensions. You can filter data with the following dimensions:    LINKED_ACCOUNT     SAVINGS_PLAN_ARN     REGION     PAYMENT_OPTION     INSTANCE_TYPE_FAMILY     GetSavingsPlansUtilizationDetails uses the same Expression object as the other operations, but only AND is supported among each dimension.
- `MaxResults`: The number of items to be returned in a response. The default is 20, with a minimum value of 1.
- `NextToken`: The token to retrieve the next set of results. Amazon Web Services provides the token when the response from a previous call has more results than the maximum page size.
"""
get_savings_plans_utilization_details(TimePeriod; aws_config::AWSConfig=global_aws_config()) = cost_explorer("GetSavingsPlansUtilizationDetails", Dict{String, Any}("TimePeriod"=>TimePeriod); aws_config=aws_config)
get_savings_plans_utilization_details(TimePeriod, args::AbstractDict{String, <:Any}; aws_config::AWSConfig=global_aws_config()) = cost_explorer("GetSavingsPlansUtilizationDetails", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("TimePeriod"=>TimePeriod), args)); aws_config=aws_config)

"""
    GetTags()

Queries for available tag keys and tag values for a specified period. You can search the tag values for an arbitrary string.

# Required Parameters
- `TimePeriod`: The start and end dates for retrieving the dimension values. The start date is inclusive, but the end date is exclusive. For example, if start is 2017-01-01 and end is 2017-05-01, then the cost and usage data is retrieved from 2017-01-01 up to and including 2017-04-30 but not including 2017-05-01.

# Optional Parameters
- `NextPageToken`: The token to retrieve the next set of results. AWS provides the token when the response from a previous call has more results than the maximum page size.
- `SearchString`: The value that you want to search for.
- `TagKey`: The key of the tag that you want to return values for.
"""
get_tags(TimePeriod; aws_config::AWSConfig=global_aws_config()) = cost_explorer("GetTags", Dict{String, Any}("TimePeriod"=>TimePeriod); aws_config=aws_config)
get_tags(TimePeriod, args::AbstractDict{String, <:Any}; aws_config::AWSConfig=global_aws_config()) = cost_explorer("GetTags", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("TimePeriod"=>TimePeriod), args)); aws_config=aws_config)

"""
    GetUsageForecast()

Retrieves a forecast for how much Amazon Web Services predicts that you will use over the forecast time period that you select, based on your past usage.

# Required Parameters
- `Granularity`: How granular you want the forecast to be. You can get 3 months of DAILY forecasts or 12 months of MONTHLY forecasts. The GetUsageForecast operation supports only DAILY and MONTHLY granularities.
- `Metric`: Which metric Cost Explorer uses to create your forecast. Valid values for a GetUsageForecast call are the following:   USAGE_QUANTITY   NORMALIZED_USAGE_AMOUNT
- `TimePeriod`: The start and end dates of the period that you want to retrieve usage forecast for. The start date is inclusive, but the end date is exclusive. For example, if start is 2017-01-01 and end is 2017-05-01, then the cost and usage data is retrieved from 2017-01-01 up to and including 2017-04-30 but not including 2017-05-01. The start date must be equal to or later than the current date to avoid a validation error.

# Optional Parameters
- `Filter`: The filters that you want to use to filter your forecast. Cost Explorer API supports all of the Cost Explorer filters.
- `PredictionIntervalLevel`: Cost Explorer always returns the mean forecast as a single point. You can request a prediction interval around the mean by specifying a confidence level. The higher the confidence level, the more confident Cost Explorer is about the actual value falling in the prediction interval. Higher confidence levels result in wider prediction intervals.
"""
get_usage_forecast(Granularity, Metric, TimePeriod; aws_config::AWSConfig=global_aws_config()) = cost_explorer("GetUsageForecast", Dict{String, Any}("Granularity"=>Granularity, "Metric"=>Metric, "TimePeriod"=>TimePeriod); aws_config=aws_config)
get_usage_forecast(Granularity, Metric, TimePeriod, args::AbstractDict{String, <:Any}; aws_config::AWSConfig=global_aws_config()) = cost_explorer("GetUsageForecast", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("Granularity"=>Granularity, "Metric"=>Metric, "TimePeriod"=>TimePeriod), args)); aws_config=aws_config)

"""
    ListCostCategoryDefinitions()

Returns the name, ARN, NumberOfRules and effective dates of all Cost Categories defined in the account. You have the option to use EffectiveOn to return a list of Cost Categories that were active on a specific date. If there is no EffectiveOn specified, you’ll see Cost Categories that are effective on the current date. If Cost Category is still effective, EffectiveEnd is omitted in the response. ListCostCategoryDefinitions supports pagination. The request can have a MaxResults range up to 100.

# Optional Parameters
- `EffectiveOn`:  The date when the Cost Category was effective.
- `MaxResults`:  The number of entries a paginated response contains.
- `NextToken`:  The token to retrieve the next set of results. Amazon Web Services provides the token when the response from a previous call has more results than the maximum page size.
"""
list_cost_category_definitions(; aws_config::AWSConfig=global_aws_config()) = cost_explorer("ListCostCategoryDefinitions"; aws_config=aws_config)
list_cost_category_definitions(args::AbstractDict{String, <:Any}; aws_config::AWSConfig=global_aws_config()) = cost_explorer("ListCostCategoryDefinitions", args; aws_config=aws_config)

"""
    ProvideAnomalyFeedback()

Modifies the feedback property of a given cost anomaly.

# Required Parameters
- `AnomalyId`:  A cost anomaly ID.
- `Feedback`: Describes whether the cost anomaly was a planned activity or you considered it an anomaly.

"""
provide_anomaly_feedback(AnomalyId, Feedback; aws_config::AWSConfig=global_aws_config()) = cost_explorer("ProvideAnomalyFeedback", Dict{String, Any}("AnomalyId"=>AnomalyId, "Feedback"=>Feedback); aws_config=aws_config)
provide_anomaly_feedback(AnomalyId, Feedback, args::AbstractDict{String, <:Any}; aws_config::AWSConfig=global_aws_config()) = cost_explorer("ProvideAnomalyFeedback", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("AnomalyId"=>AnomalyId, "Feedback"=>Feedback), args)); aws_config=aws_config)

"""
    UpdateAnomalyMonitor()

Updates an existing cost anomaly monitor. The changes made are applied going forward, and does not change anomalies detected in the past.

# Required Parameters
- `MonitorArn`:  Cost anomaly monitor Amazon Resource Names (ARNs).

# Optional Parameters
- `MonitorName`:  The new name for the cost anomaly monitor.
"""
update_anomaly_monitor(MonitorArn; aws_config::AWSConfig=global_aws_config()) = cost_explorer("UpdateAnomalyMonitor", Dict{String, Any}("MonitorArn"=>MonitorArn); aws_config=aws_config)
update_anomaly_monitor(MonitorArn, args::AbstractDict{String, <:Any}; aws_config::AWSConfig=global_aws_config()) = cost_explorer("UpdateAnomalyMonitor", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("MonitorArn"=>MonitorArn), args)); aws_config=aws_config)

"""
    UpdateAnomalySubscription()

 Updates an existing cost anomaly monitor subscription.

# Required Parameters
- `SubscriptionArn`:  A cost anomaly subscription Amazon Resource Name (ARN).

# Optional Parameters
- `Frequency`:  The update to the frequency value at which subscribers will receive notifications.
- `MonitorArnList`:  A list of cost anomaly subscription ARNs.
- `Subscribers`:  The update to the subscriber list.
- `SubscriptionName`:  The subscription's new name.
- `Threshold`:  The update to the threshold value for receiving notifications.
"""
update_anomaly_subscription(SubscriptionArn; aws_config::AWSConfig=global_aws_config()) = cost_explorer("UpdateAnomalySubscription", Dict{String, Any}("SubscriptionArn"=>SubscriptionArn); aws_config=aws_config)
update_anomaly_subscription(SubscriptionArn, args::AbstractDict{String, <:Any}; aws_config::AWSConfig=global_aws_config()) = cost_explorer("UpdateAnomalySubscription", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("SubscriptionArn"=>SubscriptionArn), args)); aws_config=aws_config)

"""
    UpdateCostCategoryDefinition()

Updates an existing Cost Category. Changes made to the Cost Category rules will be used to categorize the current month’s expenses and future expenses. This won’t change categorization for the previous months.

# Required Parameters
- `CostCategoryArn`: The unique identifier for your Cost Category.
- `RuleVersion`:
- `Rules`: The Expression object used to categorize costs. For more information, see CostCategoryRule .

"""
update_cost_category_definition(CostCategoryArn, RuleVersion, Rules; aws_config::AWSConfig=global_aws_config()) = cost_explorer("UpdateCostCategoryDefinition", Dict{String, Any}("CostCategoryArn"=>CostCategoryArn, "RuleVersion"=>RuleVersion, "Rules"=>Rules); aws_config=aws_config)
update_cost_category_definition(CostCategoryArn, RuleVersion, Rules, args::AbstractDict{String, <:Any}; aws_config::AWSConfig=global_aws_config()) = cost_explorer("UpdateCostCategoryDefinition", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("CostCategoryArn"=>CostCategoryArn, "RuleVersion"=>RuleVersion, "Rules"=>Rules), args)); aws_config=aws_config)